How to compare 3 json array in ios? - ios

Here are the 3 JSON arrays from different responses.
I just want to print "Name" in case Book which has "size_id" be 1 then print "Name" and "Discripition"
and "Prize" who has category_id & Size_id match in Objective-C (on iOS).
category
[ {
"Category_Id": "1",
" Name": "Books",
}
{
"Category_Id": "2",
" Name": "Dummies",
}
product
[
{
"Sub_Cat_Id": "3",
"Category_Id": "1",
"Size_id": "3",
"Sub_Name": "java",
"Description": "We are introducing new product.",
},
{
"Sub_Cat_Id": "4",
"Category_Id": "2",
"Size_id": "3",
"Sub_Name": "Android",
"Description": "We are introducing new product.",
},
{
"Sub_Cat_Id": "2",
"Category_Id": "1",
"Size_id": "1",
"Sub_Name": "ios",
"Description": "We are introducing new product.",
},
Prize
[
{
"Size_Id": "1",
"pirze": "600 "
},
{
"Size_Id": "2",
" pirze ": "300 "
},
{
"Size_Id": "2",
" pirze ": "800"
}

I suggest transforming this JSON into Foundation's NSArray via NSJSONSerialization's dataWithJSONObject:.
After that just use NSArray/NSDictionary filtering to retrieve your values

I suggest you to create a class for your objects (category, product and prize), so you can create an objects for your readings and add them to a NSArray. Once you've got the objects in the same array you can apply a predicate to that array, in order to choose only the objects you want:
NSArray *objects = ...; // This is the array with your objects
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(prize.size_id == 1)"];
NSArray *array = [objects filteredArrayUsingPredicate:tiltPredicate]; // This is the array with the objects you want (size_id = 1)

Related

Maintain order of elements of NSMutableDictionary [duplicate]

This question already has answers here:
Maintaining the order while iterating a NSDictionary
(3 answers)
Closed 3 years ago.
I am getting a dictionary of the following format :
[
{
"position": "1",
"name": "Jaen",
"group": "Student",
"Address": "Delhi"
},
{
"position": "2",
"name": "Jaen",
"group": "Student",
"Address": "Delhi"
},
{
"position": "1",
"name": "Jaen",
"group": "Teacher",
"Address": "Delhi"
}
]
Basically, I want to group them around the key named "group".I have to create a dictionary in the following format to populate data in my UI.
[
{
"Student": [
{
"position": "1",
"name": "Jaen",
"group": "Student",
"Address": "Delhi"
},
{
"position": "2",
"name": "Jaen",
"group": "Student",
"Address": "Delhi"
}
],
"Teacher": [
{
"position": "1",
"name": "Jaen",
"group": "Teacher",
"Address": "Delhi"
}
]
}
]
the order of the Groups should be maintained ie Student should come before Teacher. But when I am printing dictGroupField it is giving random results in terms of order.
How can i maintain the order?
Please find the code below that I was using :
#property (strong, nonatomic) NSMutableDictionary *dictGroupField;
-(void)loadFieldData {
_dictGroupField = [[NSMutableDictionary alloc] init];
NSMutableArray *arrayFields = [self getArrayFields];
for (NSDictionary *dictT in arrayFields) {
NSString *strGroup = [dict valueForKey:#"group"];
if ([_dictGroupField valueForKey:strGroup] == nil) {
NSMutableArray *arrGroup = [[NSMutableArray alloc] init];
[arrGroup addObject:dict];
[_dictGroupField setObject:arrGroup forKey:strGroup];
} else {
NSMutableArray *arrGroupExist = [_dictGroupField valueForKey:strGroup];
[arrGroupExist addObject:dict];
[_dictGroupField setObject:arrGroupExist forKey:strGroup];
}
}
}
You can't maintain it order. Dictionaries are unordered collections of key-value associations. See more Docs

Array of dictionaries: Add values if find identical in to single dictionary Swift

I have an array of structure like this
[["grocery_section": "other", "partial_quantity": "0", "name": "Ground turmeric", "unit": "teaspoons", "whole_quantity": "1"],
["grocery_section": "other", "partial_quantity": "", "name": "I", "unit": "cups", "whole_quantity": "1"],
["grocery_section": "other", "partial_quantity": "", "name": "I", "unit": "cups", "whole_quantity": "2"]]
Now, I want to find identical entries from ingredients that have the same key-value pair in that section and If found, want to add quantity into one and make one ingredient. You can check above array where the section name is "OTHER" and check ingredients there. I want to merge that 2 identical ingredients into a single one with quantity 1+1 = 2. So, the final result should be like
[["grocery_section": "other", "partial_quantity": "0", "name": "Ground turmeric", "unit": "teaspoons", "whole_quantity": "1"],
["grocery_section": "other", "partial_quantity": "", "name": "I", "unit": "cups", "whole_quantity": "3"]]
What I tried is loop here but I don't think its efficient way. So, Is there any better way to pull this thing off?
My Code
guard let first = ingredients.first else {
return [] // Empty array
}
var uniqueIngredients: [[String:String]] = [first] // Keep first element
for elem in ingredients.dropFirst() {
let equality = ingredients.compactMap { $0["name"] == elem["name"] }.count
if equality > 1 {
// SAME NAME FOR 2 INGREDIENT FOUND
// COMBINE BOTH OBJECT
} else {
// NEW NAME
// ADD NEW OBJECT
}
}
Group the array to a dictionary by name
Create a variable result
Enumerate the dictionary. If there is more than one item in value sum up the quantities and append only one item
The code assumes that the keys name and whole_quantity exist in all records and the value for key whole_quantity can be converted to Int
let array = [["grocery_section": "other", "partial_quantity": "0", "name": "Ground turmeric", "unit": "teaspoons", "whole_quantity": "1"],
["grocery_section": "other", "partial_quantity": "", "name": "I", "unit": "cups", "whole_quantity": "1"],
["grocery_section": "other", "partial_quantity": "", "name": "I", "unit": "cups", "whole_quantity": "2"]]
let groupedDictionary = Dictionary(grouping: array, by: {$0["name"]!})
var result = [[String:String]]()
for (_, value) in groupedDictionary {
if value.isEmpty { continue }
else if value.count == 1 {
result.append(value[0])
} else {
let totalQuantity = value.map{Int($0["whole_quantity"]!)!}.reduce(0, +)
var mutableValue = value[0]
mutableValue["whole_quantity"] = String(totalQuantity)
result.append(mutableValue)
}
}

fill a JSON file with embedded arrays from Rails

I have a simple "rss" (ApplicationRecord) table indexed by an id. I would like to have a structured JSON that group each user from a family in an array structure. And then each family in a global array. How can I do that ?
my current plain code to put my data in a json file is :
json.rss #rss do |rs|
json.id rs.id
json.name rs.name
json.family rs.family
json.lastdate rs.lastdate
json.last rs.last
json.s1w rs.s1w
json.s2w rs.s2w
end
But the target file that I want is this one :
{
"rss": [
{
"familyname": "Smith",
"children": [
{
"id": "1",
"name": "bob",
"lastdate": "2010-09-23",
"last": "0.88",
"s1w": "0.83",
"s2w": "0.88"
},
{
"id": 2,
"name": "Mary",
"lastdate": "2011-09-23",
"last": "0.89",
"s1w": "0.83",
"s2w": "0.87"
}
]
},
{
"familyname": "Wesson",
"children": [
{
"id": "1",
"name": "john",
"lastdate": "2001-09-23",
"last": "0.88",
"s1w": "0.83",
"s2w": "0.88"
},
{
"id": 2,
"name": "Bruce",
"lastdate": "2000-09-23",
"last": "0.89",
"s1w": "0.83",
"s2w": "0.87"
}
]
}
]
}
The grouping you are trying to achieve can be done in Ruby with:
#rss.group_by(&:family).values
This is assuming #rss is an array-like collection of objects that have a .family method. The result: is an array of arrays of objects grouped by family.
Now it will be up to use to use Jbuilder's array! method to build the desired JSON output.

How to combine hash with an array and display result as json

I'm trying to display a json after different SQL queries. Some of the SQL queries return one value but I got an array of results from one of the queries. You can see the example of the json that I want to show below.
{
"id": "4",
"potential": "23",
"conversion": "45",
"new": "34",
"repeat": "22",
"average": "14",
"traffic": [
{
"time": "9",
"new": "2",
"repeat": "1"
},
{
"time": "10",
"new": "6",
"repeat": "9"
}
]
}
I can display separately hash and array as json, however I can't combine them.
{
"id": "4",
"potential": "23",
"conversion": "45",
"new": "34",
"repeat": "22",
"average": "14"
}
AND
[
{
"time": 5,
"new": 0,
"repeat": 80
},
{
"time": 6,
"new": 1,
"repeat": 80
}
]
Any suggestions? Thank you
Traverse the response you get and add the data into a Hash. Here is a ruby-doc for it. If you need a bit idea of how to use hash see below code.
x = {"id" => "4", "potential" => "23","conversion"=> "45","new"=> "34","repeat"=> "22","average"=> "14"}
x["traffic"] =[{"time"=>5,"new"=>0,"repeat"=>80},{"time"=>6,"new"=>1,"repeat"=>80}]
You would have to use your logic to build the hash. I just wrote something for you. Also you can then return the hash by render :json => x.

Parsing Json to get all the contents in one NSArray

That's the content...
[
{
"id": "",
"title": "",
"website": "",
"categories": [
{
"id": "",
"label": ""
}
],
"updated":
},
{
"id": "",
"title": "",
"website": "",
"categories": [
{
"id": "",
"label": ""
}
],
"updated":
}
]
How can I insert every feed source in one array?
NSDictionary *results = [string JSONValue];
NSArray *subs = [results valueForKey:#"KEY"];
Which key I must insert?
THanks
as I can see your structure, you will get out of this JSON-String
NSArray:
[
NSDictionary:
{
NSString: "id",
NSString: "title",
NSString: "website",
NSArray: "categories":
[
NSDictionary:
{
NSString: "id",
NSString: "label"
}
],
NSNumber: "updated"
},
NSDictionary:
{
...
}
]
So you have already an array of "Feeds" at root and you have to itterate them with their index in the array with. For first id i.e. [[myJsonStructure objectAtIndex:0] objectForKey:#"id"];

Resources