Jongers
Jongers

Reputation: 691

Objective C - Parse json array

I want to parse parseItems but I don't know how

json = [
  {
    "devicename" : "WL-00000003",
    "activated" : "true",
    "displayname" : "wit003 ",
    "portitems" : {
      "port1" : "Smoke Sensor",
      "port4" : "",
      "port3" : "",
      "port6" : "Alarm Siren",
      "port2" : "",
      "port5" : ""
    },
    "accountType" : "admin",
    "deviceaddress" : "",
    "devicestatus" : ""
  }
]

It would be easy if portItems would be in this format "portitems" : ({ })

I tried it using this code but it gets error.

NSDictionary *fetchedDictionaryresult = [json objectForKey:@"portItems"];
    for (NSDictionary *event in  fetchedDictionaryresult)
    {
        _strPart1 = [event objectForKey:@"port1"];
        _strPart2 = [event objectForKey:@"port2"];
        _strPart3 = [event objectForKey:@"port3"];
        _strPart4 = [event objectForKey:@"port4"];
        _strPart5 = [event objectForKey:@"port5"];
        _strPart6 = [event objectForKey:@"port6"];
    }

How can I do this?

Upvotes: 0

Views: 423

Answers (3)

Nikhil Manapure
Nikhil Manapure

Reputation: 3878

Try this Here the json is first getting parsed into a dictionary and then we are getting the "portitems" dictionary and then getting strings out of it.

- (void)setPartStringsFrom:(NSData *)json
{
    NSError *localError = nil;
    NSDictionary *parsedObject = [NSJSONSerialization JSONObjectWithData:json options:0 error:&localError];

    if (localError == nil) {
        NSDictionary *event = [parsedObject[0] valueForKey:@"portitems"];

        _strPart1 = [event valueForKey:@"port1"];
        _strPart2 = [event valueForKey:@"port2"];
        _strPart3 = [event valueForKey:@"port3"];
        _strPart4 = [event valueForKey:@"port4"];
        _strPart5 = [event valueForKey:@"port5"];
        _strPart6 = [event valueForKey:@"port6"];

    }
}

and if json is already a dictionary then

 - (void)setPartStringsFrom:(NSDictionary *)json
{
    NSDictionary *event = [json[0] valueForKey:@"portitems"];

    _strPart1 = [event valueForKey:@"port1"];
    _strPart2 = [event valueForKey:@"port2"];
    _strPart3 = [event valueForKey:@"port3"];
    _strPart4 = [event valueForKey:@"port4"];
    _strPart5 = [event valueForKey:@"port5"];
    _strPart6 = [event valueForKey:@"port6"];
}

Upvotes: 1

CodeChanger
CodeChanger

Reputation: 8351

for getting portItems you did not need for loop just directly use your dictionary fetchedDictionaryresult like this:

Also I guess you got array not dictionary in Json

Try this:

NSDictionary *fetchedDictionaryresult = [json[0] objectForKey:@"portItems"];

 _strPart1 = [fetchedDictionaryresult objectForKey:@"port1"];
 _strPart2 = [fetchedDictionaryresult objectForKey:@"port2"];
 _strPart3 = [fetchedDictionaryresult objectForKey:@"port3"];
 _strPart4 = [fetchedDictionaryresult objectForKey:@"port4"];
 _strPart5 = [fetchedDictionaryresult objectForKey:@"port5"];
 _strPart6 = [fetchedDictionaryresult objectForKey:@"port6"];

Upvotes: 1

Siva Sankar
Siva Sankar

Reputation: 543

NSDictionary *dict= [responce valueforkey:@"portitems"];
NSMutableArray *portitems1=[dict valueForKey:@"port1"];
 NSMutableArray *portitems2=[dict valueForKey:@"port2"];
NSMutableArray *portitems3=[dict valueForKey:@"port3"];
NSMutableArray *portitems4=[dict valueForKey:@"port4"];
NSMutableArray *portitems5=[dict valueForKey:@"port5"];                                                                              

Upvotes: 1

Related Questions