-1

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.

Santo
  • 1,611
  • 3
  • 17
  • 37

3 Answers3

11

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).

enter image description here

An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).

enter image description here

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.

enter image description here

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.

enter image description here

A number is very much like a C or Java number, except that the octal and hexadecimal formats are not used.

enter image description here

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

user3182143
  • 9,459
  • 3
  • 32
  • 39
  • Please, can you explain why 'family' key-value pair is printed before 'name', as you can see the 'name' is before 'family' in JSON string? I am asking this because I am facing this problem with my data having keys as Unicode characters(Hindi Language). After converting to JSON the keys are changing their orders. I have also noticed they are getting sorted on basis of their Unicode. – iHulk Dec 22 '17 at 10:22
1

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);
vaibhav
  • 4,038
  • 1
  • 21
  • 51
0

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.

  1. Remove \n, \, and ;
  2. Replace = with :.
  3. Add double quotes "" for every string (e.g., from name to "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+)

Community
  • 1
  • 1
ramacode
  • 914
  • 6
  • 14