问题
I tired with links but those are not working in my case.
Can anyone suggest me on how to convert this string to json or dictionary.
This string is having a dictionary with an array of strings.
NSString *temp=@"{\n name = {\n dob = \"\";\n age = \"61\";\n family = (\n {\n location = location;\n mobile = mobile;\n }\n );\n };\n}";
Here family is an array.
回答1:
I got the solution.
First I tried your code
NSString *temp=@"{\n name = {\n dob = \"\";\n age = \"61\";\n family = (\n {\n location = location;\n mobile = mobile;\n }\n );\n };\n}";
My out put result is
null
Then I changed the JSON format.
So now your code should be
NSString *strJson=@"{\"name\":{\"dob\":88,\"age\":61},\"family\" : [{\"location\":\"us\",\"mobile\":\"mobile\"}]}";
NSData *data = [strJson dataUsingEncoding:NSUTF8StringEncoding];
id jsonOutput = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@",jsonOutput);
The printed result is
{
family = (
{
location = us;
mobile = mobile;
}
);
name = {
age = 61;
dob = 88;
};
}
You have to remember that when you convert string to json
\n must be \"value\"
= must be :
( must be [
number must be 100(or anything else)
An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).
An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).
A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.
A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.
A number is very much like a C or Java number, except that the octal and hexadecimal formats are not used.
Finally
Whitespace can be inserted between any pair of tokens. Excepting a few encoding details, that completely describes the language.
I got this useful data from this
回答2:
using NSJSONSerialization class and its method JSONObjectWithData you can convert string to dict or json.
NSData *data = [yourString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
// OR
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"string: %@",json);
回答3:
First of all your JSON string has invalid format. You can refer here to see the correct format (http://json.org/). You can check http://jsonviewer.stack.hu/ see if it is working.
I tried to clean your JSON string manually.
- Remove
\n,\, and; - Replace
=with:. - Add double quotes
""for every string (e.g., fromnameto"name").
Recheck again and after the format is valid then you can use @vaibhav answer to convert NSString to NSDictionary. Or see here How do I deserialize a JSON string into an NSDictionary? (For iOS 5+)
来源:https://stackoverflow.com/questions/39873257/how-to-convert-this-string-to-json-or-dictionary-in-ios