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"]);
Related
How to remove backslash(/)?. Every parameter is coming with backslash.All values stored in dictionary
ex:getCategoryListReturn" :"[{\"categoryId\":734, \"categoryNm\":\"Projector Dealers\",\"categoryNmBl\":\"تجار العارض\/مسلاط\",\"prntCategoryId\":733},{\"categoryId\":735,\"categoryNm\":\"Computer Dealers\",\"categoryNmBl\":\"تجار الحاسب الآلي \",\"prntCategoryId\":733},{\"categoryId\":736,\"categoryNm\":\"Laptop Dealers\",\"categoryNmBl\":\"تجار الحاسب الآلي\",\"prntCategoryId\":733},{\"categoryId\":737,\"categoryNm\":\"Printer Dealers\",\"categoryNmBl\":\"الطباعة\",\"prntCategoryId\":733},{\"categoryId\":738,\"categoryNm\":\"Colour Printer Dealers\",\"categoryNmBl\":\"تجار الطباعة الملونة\",\"prntCategoryId\":733},{\"categoryId\":739,\"categoryNm\":\"Laser Printer Dealers\",\"categoryNmBl\":\"الطباعة بالليزر\",\"prntCategoryId\":733},{\"categoryId\":740,\"categoryNm\":\"All In One Printer Dealers\",\"categoryNmBl\":\"اعمال الطباعة \",\"prntCategoryId\":733},{\"categoryId\":741,\"categoryNm\":\"Colour Laser Printer Dealers\",\"categoryNmBl\":\"الطباعة الملونة\",\"prntCategoryId\":733},{\"categoryId\":}
Follow the below code to remove backslash(/n)
NSData *data1 = [[dictionary valueForKey:#"getCategoryListReturn"] dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data1 options:0 error:nil];
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);
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);
In the following code for parsing a JSON, when objects happen to be other than NSArray or NSDictionary, NSJSONReadingAllowFragments is used.
The below code works fine and prints out 32. but if replace 32 by abcd, it outputs to null.
Any idea why its null for a string abcd.
NSString *num=#"32";
NSError *error;
NSData *createdData = [num dataUsingEncoding:NSUTF8StringEncoding];
id response=[NSJSONSerialization JSONObjectWithData:createdData options:NSJSONReadingAllowFragments error:&error];
NSLog(#"Response= %#",response);
That’s because 32 is a valid JSON fragment (a number), but abcd is not a valid JSON fragment since all strings must be quoted. Try:
NSString *num=#"\"abcd\"";
which produces "abcd" instead of abcd.
(also, bear in mind that the object being returned is not really an NSDictionary, so declaring it as id makes more sense)
I am parsing a JSON url. I am getting \u00e2\u20ac\u0153 instead of single quotes and \u00e2\u20ac\u009d instead of double quotes. On my end i am doing NSUTF8StringEncoding but still not getting single and double quotes.
Here is the response in the web browser:
{"status":"1","data":[{"id":"1345","title_en":"","content_en":"Mom
said, \u00e2\u20ac\u0153I love this one.\u00e2\u20ac\u009d\n\nJake
said, \u00e2\u20ac\u0153I don\u00e2\u20ac\u2122t get
it.\u00e2\u20ac\u009d\n","image":"1396","pos":"2","video_type":"0","video":"0","video_alt":"","sound_type":"1","sound":"1123","sound_alt":"","author":"","type":"0","book_id":"148","user_id":"24","title":"The
Art
Museum","author_url":"maureen-d-1","book_url":"the-art-museum","video_filename":"","sound_filename":"atmuseum2.m4a"}]}
Is it possible to parse above response using NSUTF8StringEncoding or something else. Or i need to get it done from php side.
Do you use NSJSONSerialization? In my app i do this:
NSError* localError;
id jsonObjects = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:&localError];
In "data" variable i have response from server. I receiving UTF-encoded text from server too (like \u00e2\u20ac) but finally i get normal string. "jsonObjects" will contain data in native types:
if ([jsonObjects isKindOfClass:[NSDictionary class]])
{
NSNumber* age = [jsonObjects objectForKey:#"age"];
//...etc
}