Convert String(already formatted) to an Array of NSDictionary - ios

I have this String already formatted as a [[String:AnyObject]], however I can't retrieve the information out of it nor couldn't find a way to convert it to a dictionary.
[ [name: Laura, age:33, gender: female], [name: James, age:25,
gender:male] ]
What can be done to convert this string into a dictionary?

Your formatted string must be well formatted as an json, then you can use the next code:
NSString* str=#"[ {\"name\": \"Laura\", \"age\":33, \"gender\": \"female\"}, {\"name\": \"James\", \"age\":25, \"gender\":\"male\"} ]";
NSData* data=[str dataUsingEncoding:NSUTF8StringEncoding];
NSArray* jsonArr=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(#"%#",jsonArr);

Related

I want to convert a specific string to json

server response
-> NSString : { key1 = value1 }{ key2 = value2 }{ key3 = value3 }
How do I convert it to json?
What should I do?
I think what you are looking for is something like this , but im not suer if you can use = isntead of :
NSString *stringValue =#"[{ \"Key1\":\"Value1\" },{ \"Key2\":\"Value2\" },{ \"Key3\":\"Value3\" }]";
NSLog(#"StringValues from Server=%#", stringValue);
// Convert to JSON object:
NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:[stringValue dataUsingEncoding:NSUTF8StringEncoding]
options:0 error:NULL];
NSLog(#"jsonObject=%#", jsonObject);
If this is not what you want try to give us some code and explain how you want it done

Weird sorting when parsing JSON

I am working on an App and I use my JSON RESTful API. Therefor I parse JSON like in the following example:
NSData * apiReturn;
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:apiReturn options:NSJSONReadingMutableLeaves error:&myError];
If I now pass the following JSON encoded String via the API to the App:
{"da": {"name":"dienstlicher Anlass", "duty":true, "type":"dropdown",
"values":{"gt":{"name":"Gesprächstermin", "recurring":true,
"values":{"fa":{"name":"Firma", "duty":true, "type":"text",
"multiline":false}, "ar":{"name":"Anrede", "duty":true, "single":true,
"type":"checkbox", "values":{"fr":{"name":"Frau"},
"hr":{"name":"Herr"}}}, "nm":{"name":"Gesprächspartner", "duty":true,
"type":"text", "multiline":false}, "bm":{"name":"Bemerkung",
"duty":false, "type":"text", "multiline":true}}}}
},"bm":{"name":"Bemerkung", "values":[],
"type":"text"},"bm2":{"name":"Bemerkung", "values":[], "type":"text"}}
(You can parse this a bit prettier here: http://json.parser.online.fr)
The NSDictionary should contain the following Keys: "da", "bm", "bm2" - Sorted this way, because this is the sorting of the JSON String!
But what Objective-C does, is this:
Why does Objective-C sort the elements, and why so wrong?
I hope you can help! :)
An NSDictionary is not guaranteed to store the keys in any given order.

Converting JSON string into object

I am trying to convert a JSON string into an object in Objective C using the code:
NSString *jsonString = (NSString *) responseObject;
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
The value of jsonString is
"[{\"Date\": \"12-01-2015\", \"Time\": \"7:00 am\", \"Title\":
\"First Car Service\", \"Details\": \"This was the first car
service ever done\"}]"
But the value of json is always nil. How do I convert the jsonString into a NSArray ?
My (third) bet: your json string isn't correct. If it contains the leading and trailing quotes strip them. Replaced the \" with ".
better: make the server or other json source send correct json.
From the error, it may be that your string is not formatted correctly, probably due to the quotes.
You should also make sure that after you have formatted the string, then you check the array has objects.
NSLog(#"JSON Details: %#", json[0][#"Details"]);

JSon array to core data entity

I have a cuestion about parsing a Json strings.
If i'm getting a string like:
[["AR","Argentina","flag_of_argentina","en","F","1"],
["AU","Australia","flag_of_‌​australia","en","B","4"]]
How can i save this string into an entity when normally i see Jsons that have something like this:
(
(
"Group_id": 1,
"Date" : "2014-04-08",
"Hour" : "18:00:00",
"Location":"Guayaquil, Ecuador",
"State" : A
),
...
If i have to make an array or something else to store it to the entity in core data how can i do it?
I hope my question is well made ​​and I appreciate your help. Thank you very much.
Cocoa Touch provides a built in class that helps you serialize this data. It's called NSJSONSerialization.
So, that looks to me like you have arrays within an array. First you're going to want to convert the NSString to NSData:
NSData *JSONdata = [yourJSONString dataUsingEncoding:NSUTF8StringEncoding];
Then serialize it into an array:
NSArray *JSONArray = [NSJSONSerialization JSONObjectWithData:JSONdata options:NSJSONReadingMutableContainers error:&error];
Then you can access any subarray by writing:
NSArray *countryData = [JSONArray objectAtIndex:i];
And from there you can access the data that you need.
~
As for building JSON Data, you'll need to construct an NSMutableDictionary and populate it accordingly. Once you have that, use the same class NSJSONSerialization to convert it to a JSON string.

iOS: Parsing dictionaries from a string into NSMutableDictionary / NSMutableArray

Is there a fast way to parse dictionaries in iOS from a sting, with the format:
property1: value1
property2: [ property3: value3 ; property4: [property5: value5] ]
property6: "and so on"
The string would contain something like:
NSString *str = #"property1: value1 property2: [ property3: value3 ; property4: [property5: value5]] property6: "and so on" ";
and would generate a root NSMutableDictionary / NSMutableArray element, containing additional
NSMutableDictionary / NSMutableArray elements
Thanks in advance
why not using json format ?
replace this with this
[ {
] }
; ,
and you will be able to use jsonparser : NSJSONSerialization
No. That isn't any standard format. It looks like a bizarro version of JSON. If you want to parse this format, you'll need to either find some third-party parser or wrote one yourself.
Your JSON string is invalid. Please use http://jsonlint.com to verity if your JSON string is valid or not. Now, given your JSON is valid, this is how you can read string JSON into a NSDictionary:
NSString *strData = #"{\"property1\":\"value1\",\"property2\":{\"property3\":\"value3\",\"property4\":\"value4\"}}";
NSData *data = [strData dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(#"%#", json);

Resources