SwiftyJson is it possible to iterate through objects but not arrays? - swift

I have object
{"FIELD NAME":{
"SUBNAME":{
"FIELD_ONE":"DATA",
"FIELD_TWO":"DATA",
}
"SUBNAME2":{
"FIELD_THREE":"DATA",
"FIELD_FOUR":"DATA",
}
}
I want to get data from SUBNAME and SUBNAME2. Problem is that I dont know name of these fields and how many of them there are.
Is it possible to iterate through them using SwiftJSON ?

After converting your JSON to an object, you can do this:
for (subname, dictionary) in json {
// use subname and the dictionary, which contains the FIELD_* entries
}

Related

Get keys From Interface

I know the title says interface array, but that's how the data is being printed.
I have a field in mongodb called "devices" which is of type object. This object contains a bunch of random key values pairs. The keys are just randomly generated integers and the value are a string.
So I've written some code which retrieves the data from the db, after I've retrieved the data I want to get all the keys from theses objects and store them in an array and I can't seem to figure out how to do that.
First I assume my issue lies with the way I'm getting the data since I'm storing it into an interface{} and not an array. The data prints out like an array but when I change the structure to retrieve an array it comes back empty so I scrapped that idea.
Function
type Data struct {
Devices interface{} `json:"devices" bson:"devices"`
}
client := db.ConnectClient()
col := client.Database("Users").Collection("User")
var deviceIds Data
_ = col.FindOne(context.TODO(), bson.D{}).Decode(&deviceIds)
log.Print(deviceIds.Devices)
Output
2020/10/29 21:28:07 [{123456789 Plant} {456753121 Money Bringer} {798745321 Hello}]
Also I have tried changing that struct to
type Data struct {
Devices map[int]interface{} `json:"devices" bson:"devices"`
}
which gives an output of
2020/10/29 21:35:10 map[123456789:Plant 456753121:Money Bringer 798745321:Hello]
but again I don't know how to extract the keys from them
So as I just finished writing this question I figured out the map was the correct way to get my data.
Posted the question and answering it myself because I thought it would be useful for others
type Data struct {
Devices map[int]interface{} `json:"devices" bson:"devices"`
}
for key, value := range deviceIds.Devices {
fmt.Println(key, value)
}
Changing the struct to the above map and then looping through it with range worked perfectly

How to add Nested Ordered Dictionary in IOS Swift?

I have a sample Nested dictionary like:
var sampleDictionary = ["section1": [["question1","notes1"],["question2","notes2"],["question2","notes2"],["question3","notes3"]],"section2": [["questionS2Q1","notesS2N1"],["questionS2Q2","notesS2N2"],["questionS3Q3","notesS2Q3"],["question3","notes3"]]]
I want to create a new list which will look like this dictionary by using a loop.
After performing a nested loop I should be able to append the values which will result like this:
Loop_Content_append = ["Section1": [["question1","notes1"],["question2","notes2"],["question3","notes3"]]]
//[Key1 : [[Array],[Array]],key2:[[Array],[Array]]]
So My Key is Section1 and Value is an array of Dictionary.
But I am not able to define it as empty or append inside loop.
How can I implement this ?
Please help.

Meteor: Store many objects in one object

What is the optimal way to store a set of objects in a meteor collection?
E.g.
Container.insert({
name: "A",
list: [obj1, obj2, obj3]
});
It seems like container.list would return an array, but what if given obj1, I wanted to find the Container.name?
It's hard to give a precise answer without a schema for the objects in list, but imagine each had an _id property. Then you could do something like:
let name = Container.findOne({'list._id': 'abcd'}).name;

How to keep a groupedby list sorted in Play Framework 2 templates

I've got a list of complex objects that I want to display, grouped by one of its attribute in a Play 2 template.
I managed to do it :
#measures.groupBy(_.question.category).map {
case (category, items) => {
// Category stuff
#for(item <- items) {
// List of items
}
}
}
The problem is that the list was sorted in my Java controller, but the keyset of the map that I create is not sorted anymore (I would like to sort the key set using something like _.question.category.order).
Is there a way to have a sorted map on this attribute?
Thanks !
What type is measures? Did you try to use a LinkedHashSet? This should keep the elements order in contrast to e.g. a HashSet.

Combining objects in an array

I have one array that is pre-filled with dictionaries with each dictionary consisting of two keys.
{
name = object1;
quantity = 5;
},
{
name = object2;
quantity = 2;
}
I want to be able to add more objects and combine any duplicate dictionaries. So for example, if I added 5 of object one and 3 of object 2, this would be the result.
{
name = object1;
quantity = 10;
},
{
name = object2;
quantity = 5;
}
My ultimate goal is to be able to display the quantity next to the item in a table view. What is the best way to accomplish this?
Sounds like you want an NSCountedSet.
If you really want to iterate through your existing structure then you could do something like:
for (NSMutableDictionary *dict in ArrayName) {
if([[dict valueForKey:#"name"] isEqual:someobject]) {
int oldValue = [[dict valueForKey:#"quantity"] intValue];
[dict setValue:[NSString stringWithFormat:#"%d",oldValue+1] forKey:#"quantity"];
}
}
I assumed you stored the quantity as an NSString, you may have used something else like NSNumber.
I think a better idea is create your own class with two properties, a name and a quantity. Then hold one array of those objects and iterate through that.
Well since the keys for your dictionaries always look the same, I'd wonder if that's not actually a different dictionary:
{ object1: 10, object2: 5}
Assuming your object names are valid keys, I'd use those as keys, and then just increment the counters as needed when you get more objects to add.
I do not have a clear idea of what you are exacly doing but,
you may solve this problem with just a dictionary (without an array) that stores you objects as its key and their count as its value.
so when you want to add an object, you first check to see if you alerady have that object
-if you dont have it you add object1=1
-if you have it then you increment the value of that object1=2