Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decode JSON to NSArray or NSDictionary

I hope to decode the JSON data below:

{
    "content":
    [   
        {
            "1":"a",
            "2":"b",
            "3":"c",
            "4":"d",
            "mark":"yes"
        }
    ]
}

Not sure if put it in NSArray or NSDictionary

Welcome any comment

like image 507
arachide Avatar asked Apr 12 '12 10:04

arachide


People also ask

What is fromJson and toJson in flutter?

Serializing JSON inside model classes Inside the User class, you'll find: A User.fromJson() constructor, for constructing a new User instance from a map structure. A toJson() method, which converts a User instance into a map.

What is JSONPath parse?

JSONPath is an expression language to parse JSON data. It's very similar to the XPath expression language to parse XML data. The idea is to parse the JSON data and get the value you want.

How to convert Data to JSON in objective c?

Converting to JSON You can use jsonStringor jsonDatato get the NSStringor NSDataencoded versions in JSON respectively. NSData *jsonData = [d jsonData]; Both methods are available on NSNull , NSNumber , NSArray , NSDictionary , NSObject , and NSString .

What is JSONSerialization in Swift?

You use the JSONSerialization class to convert JSON to Foundation objects and convert Foundation objects to JSON. To convert a Foundation object to JSON, the object must have the following properties: The top level object is an NSArray or NSDictionary , unless you set the fragmentsAllowed option.


2 Answers

which iOS version are you using? in iOS 5 you have the NSJSONSerialization class to parse JSON data, if you need to target older iOSs or MAC OSX you should use third parties lib such as SBJSON. The string posted will be a NSDictionary with an array with one dictionary. The array will be accessible using the key @"content"

In code:

NSString * jsonString = @"blblblblblb";
NSStringEncoding  encoding;
NSData * jsonData = [jsonString dataUsingEncoding:encoding];
NSError * error=nil;
NSDictionary * parsedData = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];

In SWIFT 2.0:

    let jsonString = "blblblblblb"
    let encoding = NSUTF8StringEncoding
    let jsonData = jsonString.dataUsingEncoding(encoding)
    guard let jData = jsonData else {return}
    do {
        let parsedData = try NSJSONSerialization.JSONObjectWithData(jData, options: [])
    } catch let error {
        print("json error: \(error)")
    }

[UPDATE] The NSJSONSerialization class is also available for 10.7 my comment wasn't correct.

like image 121
Andrea Avatar answered Oct 29 '22 01:10

Andrea


That particular string will decode into an NSDictionary because the outermost thing is a JSON object which maps onto a NSDictionary for every JSON implementation I have ever seen. If you want to process an arbitrary string, you'll need to test what you get back

NSError *jsonError;
id parsedThing = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
if (parsedThing == nil)
{
    // error
}
else if ([parsedThing isKindOfClass: [NSArray class]])
{
    // handle array, parsedThing can be cast as an NSArray safely
}
else
{
    // handle dictionary, parsedThing can be cast as an NSDictionary
    // NB only dictionaries and arrays allowed as long as NSJSONReadingAllowFragments 
    // not specified in the options
}
like image 28
JeremyP Avatar answered Oct 28 '22 23:10

JeremyP