Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

how to convert this string to json or dictionary in ios

Writer Andrew Mclaughlin

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.

3

3 Answers

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

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

First of all your JSON string has invalid format. You can refer here to see the correct format (). You can check 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+)

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy