how to create nsmutable array with object - ios

I want to create an array like this:
"F017A_order_detail" = (
{
id = 50158;
"is_rx_medicine" = 1;
"medicine_type_id" = 2;
name = "1-11~qwe";
"order_id" = 50128;
price = "<null>";
qty = 12;
strengh = "12 mcg";
},
{
id = 50159;
"is_rx_medicine" = 1;
"medicine_type_id" = 3;
name = "1-12~qwe";
"order_id" = 50128;
price = "<null>";
qty = 12;
strengh = "12 ng/dL";
}
);
How can I create an array with object and key value pair?
How can I add values into the object when I want to add?

Related

JSON object data to UILabel

How can I populate a few UILabels on a ViewController from the JSON object I receive from a previously passed id parameter? I get the response from the server but, I don't know how to populate the key/value pairs to a UILabel.
The JSON response I get is:
{
product = {
attributes = (
);
"average_rating" = "0.00";
backordered = 0;
"backorders_allowed" = 0;
"button_text" = "";
"catalog_visibility" = visible;
categories = (
);
"created_at" = "2016-02-23T05:00:55Z";
"cross_sell_ids" = (
);
description = "<p>Microsoft Office</p>\n";
dimensions = {
height = "";
length = "";
unit = in;
width = "";
};
"download_expiry" = 0;
"download_limit" = 0;
"download_type" = "";
downloadable = 0;
downloads = (
);
featured = 0;
"featured_src" = "https://qashop.taxometry.com/wordpressv4/wp-content/uploads/2016/02/softwarecontract-150x150.jpg";
"grouped_products" = (
);
id = 15;
images = (
{
alt = "";
"created_at" = "2016-02-23T05:01:35Z";
id = 16;
position = 0;
src = "https://qashop.taxometry.com/wordpressv4/wp-content/uploads/2016/02/softwarecontract-150x150.jpg";
title = "softwarecontract-150×150";
"updated_at" = "2016-02-23T05:01:35Z";
}
);
"in_stock" = 1;
"managing_stock" = 0;
"menu_order" = 0;
"on_sale" = 1;
parent = (
);
"parent_id" = 0;
permalink = "https://qashop.taxometry.com/wordpressv4/product/software-contract/";
price = 25;
"price_html" = "<del><span class=\"amount\">$30.00</span></del> <ins><span class=\"amount\">$25.00</span></ins>";
"product_url" = "";
"purchase_note" = "";
purchaseable = 1;
"rating_count" = 0;
"regular_price" = 30;
"related_ids" = (
);
"reviews_allowed" = 1;
"sale_price" = 25;
"shipping_class" = "";
"shipping_class_id" = "<null>";
"shipping_required" = 1;
"shipping_taxable" = 1;
"short_description" = "";
sku = 2370;
"sold_individually" = 0;
status = publish;
"stock_quantity" = "<null>";
tags = (
);
"tax_class" = "";
"tax_status" = taxable;
taxable = 1;
title = "Software Contract";
"total_sales" = 5;
type = simple;
"updated_at" = "2016-02-23T05:01:41Z";
"upsell_ids" = (
);
variations = (
);
virtual = 0;
visible = 1;
weight = "<null>";
};
}
Try this - > First
then Second

remove Array object content dictionary with null value iOS

I have Json object like this :
{
CO = 1;
CUABR = "<null>";
CUNO = "<null>";
DIV = 2;
DLDAT = 1020923;
SALID = "<null>";
"form_number" = MM23000;
type = VHNO;
},
{
CO = 1;
CUABR = "<null>";
CUNO = "<null>";
DIV = 2;
DLDAT = 1020923;
SALID = "<null>";
"form_number" = MM23001;
type = VHNO;
},
{
CO = 1;
CUABR = "<null>";
CUNO = "<null>";
DIV = 2;
DLDAT = 1020924;
SALID = "<null>";
"form_number" = MM23002;
type = VHNO;
},
{
CO = 1;
CUABR = "\U53f0\U5316\U65b0\U6e2f";
CUNO = FCFC3;
DIV = 2;
DLDAT = 1020924;
SALID = M;
"form_number" = MM23003;
type = VHNO;
}
}
then I need to remove json array content null dictionary key value to sort like this:
{
CO = 1;
CUABR = "\U53f0\U5316\U65b0\U6e2f";
CUNO = FCFC3;
DIV = 2;
DLDAT = 1020924;
SALID = M;
"form_number" = MM23003;
type = VHNO;
}
}
How should I do?
I would like to know the proper way, should I use nest loop or block?
Thanks for helping.
use this may help you..
NSMutableArray *newarray=[NSMutableArray array];
for (NSDictionary *dictionary in parseArrray)
{
if (dictionary[key] != [NSNull null]) {
[newarray addObject:dictionary];
}
}

Add Array in nsmutableaary

i am developing instragramm type app when user tap like button i want add username in likes arry
So I have a dictionary in a dictionary with arrays. I am trying to add to the arrays that are in the likes array.
{
caption = "Shakira Shakira...";
comments = (
);
"display_name" = Shakira;
ext = "<null>";
"image_id" = 53;
likes = (
);
"post_type" = i;
"total_comments" = 0;
"total_likes" = 0;
"upload_dt" = 3wk;
"user_id" = 7;
"user_liked" = 0;
},
{
caption = "Addicted to Shakira???";
comments = (
{
comment = "love your profile :)";
"display_name" = Rihanna;
"user_id" = 5;
},
{
comment = "You are my favorite celebrity on Follow Me.";
"display_name" = Pitbull;
"user_id" = 4;
},
{
comment = "Looking Good!";
"display_name" = "Justin Bieber";
"user_id" = 2;
}
);
"display_name" = Shakira;
ext = "<null>";
"image_id" = 52;
likes = (
{
"display_name" = "Justin Bieber";
"user_id" = 2;
},
{
"display_name" = Rihanna;
"user_id" = 5;
},
{
"display_name" = Pitbull;
"user_id" = 4;
}
);
"post_type" = i;
"total_comments" = 3;
"total_likes" = 3;
"upload_dt" = 2mth;
"user_id" = 7;
"user_liked" = 0;
},
i want add display_name and user_id
NSMutableArray *mutearr = [NSMutableArray arrayWithArray:array];
here array is NSArray and mutarr is NSMutablearray
You can use addObjectsFromArray: from NSMutableArray class
NSArray *animal = [[NSArray alloc]initWithObjects:#"Lion",#"Tiger",#"Dog",#"Cat",#"Sheep",#"Wolf", nil];
NSArray *color = [[NSArray alloc]initWithObjects:#"Blue",#"Red",#"Yellow",#"Green",#"Black", nil];
NSMutableArray *all = [[NSMutableArray alloc] init];
[all addObjectsFromArray:animal];
[all addObjectsFromArray:color];
var displayNames = [String]()
var userIds = [Int]()
let responseArray = (yourArray as? [NSDictionary]) ?? []
for ar in responseArray{
let display_name = ar.value(forKey: "display_name") as? String
displayNames.append(display_name ?? "")
let user_id = ar.value(forKey: "user_id") as? Int
userIds.append(user_id ?? 0)
}
print(displayNames,userIds)

Issue with Sorting NSMutableArray

I have an NSMutableArray which contains an NSDictionary, I need to sort the array based on certain criteria.
Sorting criteria is
sort based on properties.property.value, suppose if I want to sort based on properties.property.name = Risk value. I want all the elements with Risk value first with the value in Ascending order. That means for the Dictionary which doesn't have
properties.property.name = Risk must come last. Some of the dictionaries don't have these names.
Please help...
finalArr (
{
ViewTag = 101;
"action-taken-date" = {
class = "java.util.GregorianCalendar";
text = "2013-06-03 22:23:28.0 PDT";
};
alarms = 0;
alerts = 0;
application = {
assignee = "Test Name";
completed = false;
"properties" = {
class = "java.util.ArrayList";
property = (
{
label = Risk;
name = Risk;
value = "3 - Low";
},
{
label = "Start Time";
name = "Start Time";
value = "05/09/13 06:00:00";
}
);
};
{
ViewTag = 102;
"date" = {
class = "java.util.GregorianCalendar";
text = "2013-06-03 22:23:28.0 PDT";
};
alarms = 0;
alerts = 0;
application = {
assignee = "Test Name";
completed = false;
"properties" = {
class = "java.util.ArrayList";
property = (
{
label = Risk;
name = Risk;
value = "2 - Low";
},
{
label = "Start Time";
name = "Start Time";
value = "05/09/13 06:00:00";
}
);
};
{
ViewTag = 103;
"date" = {
class = "java.util.GregorianCalendar";
text = "2013-06-03 22:23:28.0 PDT";
};
alarms = 0;
alerts = 0;
application = {
assignee = "Test Name";
completed = false;
"properties" = {
class = "java.util.ArrayList";
property = (
{
label = Status;
name = Status;
value = "Pending Signoffs";
},
{
label = Priority;
name = Priority;
value = 3;
}
);
};
{
ViewTag = 104;
"date" = {
class = "java.util.GregorianCalendar";
text = "2013-06-03 22:23:28.0 PDT";
};
alarms = 0;
alerts = 0;
application = {
assignee = "Test Name";
completed = false;
"properties" = {
property = (
{
label = Priority;
name = priority;
value = 1;
},
{
label = "Start Time";
name = "Start Time";
value = "05/09/13 06:00:00";
}
);
};
{
ViewTag = 103;
"date" = {
class = "java.util.GregorianCalendar";
text = "2013-06-03 22:23:28.0 PDT";
};
alarms = 0;
alerts = 0;
application = {
assignee = "Test Name";
completed = false;
"properties" = {
property = (
{
label = Status;
name = Status;
value = "Pending Signoffs";
},
{
label = Priority;
name = Priority;
value = 2;
}
);
};
)
Try with this
NSString *risk = #"Risk";
[finalArray sortUsingComparator:^NSComparisonResult(NSDictionary * dict1, NSDictionary * dict2) {
NSDictionary *property1 = dict1[#"properties"][#"property"][0];
NSString *name1 = property1[#"name"];
NSDictionary *property2 = dict2[#"properties"][#"property"][0];
NSString *name2 = property2[#"name"];
if ([name1 isEqualToString:name2]) {
//if both names are equal then they are sorted based on value
NSString *value1 = property1[#"value"];
NSString *value2 = property2[#"value"];
return [value1 compare:value2 options:NSNumericSearch];
}else if ([name1 isEqualToString:risk]){
//if name1 is equal to risk it is moved up
return NSOrderedAscending;
}else{
return NSOrderedDescending;
}
}];
It sounds like you have multiple different requirements (logic) which go into determining the desired sort result. You should write your own comparison logic and use one of the array methods like sortUsingComparator:.

NSMutableArray of dictionaries from JSON feed insert new object for key iOS

I receive and array of dictionaries from a JSON feed and assign it to an NSMutableArray called jsonArray i.e:
jsonArray = [deserializedData objectForKey:#"reports"];
The feed looks like this:
reports = (
{
address = "The street";
email = "alex#blah.co.uk";
"eng_id" = 1;
"eng_name" = "Alex McPherson";
"eng_thumb" = "http://someurl/image/1.png";
form = Test;
id = 59;
lat = "51.1438330";
live = 1;
lng = "0.8693330";
location = "17 Victoria Crescent, Ashford, TN23 7HL";
name = "Alex McPherson";
phone = 01233000000;
rid = "A5C963-C95B-C3D639";
title = "#A5C963-C95B-C3D639, Litter";
tm = "2013-04-28 20:44:20";
type = 5;
"type-text" = "Litter";
},
{
address = "The street";
email = "alex#blah.co.uk";
"eng_id" = 2;
"eng_name" = "Rob Burt";
"eng_thumb" = "http://someurl/image/1.png";
form = Test;
id = 122;
lat = "51.1415000";
live = 1;
lng = "0.8715000";
location = "38 Beaver Road, Ashford, TN23 7RP";
name = Alex;
phone = 01233000000;
rid = "5A5C96-9072-6BAFA9";
title = "#5A5C96-9072-6BAFA9, Litter";
tm = "2013-04-28 20:35:56";
type = 8;
"type-text" = "Litter";
};
what I would like to do is insert a new value for key lets say into this jsonArray: distance = "0.16km" but my brain is just not working tonight....
so the new jsonArray should looks like this with the added key value mentioned above:
reports = (
{
address = "The street";
email = "alex#blah.co.uk";
"eng_id" = 1;
"eng_name" = "Alex McPherson";
"eng_thumb" = "http://someurl/image/1.png";
form = Test;
id = 59;
lat = "51.1438330";
live = 1;
lng = "0.8693330";
distance = "0.16km";
location = "17 Victoria Crescent, Ashford, TN23 7HL";
name = "Alex McPherson";
phone = 01233000000;
rid = "A5C963-C95B-C3D639";
title = "#A5C963-C95B-C3D639, Litter";
tm = "2013-04-28 20:44:20";
type = 5;
"type-text" = "Litter";
},
{
address = "The street";
email = "alex#blah.co.uk";
"eng_id" = 2;
"eng_name" = "Rob Burt";
"eng_thumb" = "http://someurl/image/1.png";
form = Test;
id = 122;
lat = "51.1415000";
live = 1;
lng = "0.8715000";
distance = "2.13km";
location = "38 Beaver Road, Ashford, TN23 7RP";
name = Alex;
phone = 01233000000;
rid = "5A5C96-9072-6BAFA9";
title = "#5A5C96-9072-6BAFA9, Litter";
tm = "2013-04-28 20:35:56";
type = 8;
"type-text" = "Litter";
};
basically I have a calculation that takes the long and lat from the feed and works out how far the poi is from my current location of which then I sort the array by using a sort descriptor based on the distance key that I want to insert above. I have the code for this just stuck on inserting into the existing nsmutablearray above
Read the deserialized JSON array as an NSArray (not NSMutableArray).
Then create a mutable copy of that array using something like:
NSMutableArray *mutableArray = [originalArray mutableCopy];
Then insert items into mutableArray.

Resources