0

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?

Jongers
  • 595
  • 1
  • 9
  • 29

3 Answers3

1

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"];
}
Nikhil Manapure
  • 3,748
  • 2
  • 30
  • 55
  • Check [this](http://stackoverflow.com/questions/1062183/difference-between-objectforkey-and-valueforkey) out. – Nikhil Manapure Dec 02 '16 at 09:23
  • It was probably objectForKey and valueForKey that made the error. – Jongers Dec 02 '16 at 09:33
  • Don't use `valueForKey` unless you know what KVC is and you really need its functionality. – vadian Dec 02 '16 at 09:34
  • @vadian could you direct us to some good documentation about KVC and make us understand what it means. This would be helpful. – Nikhil Manapure Dec 02 '16 at 09:35
  • Short description: [Key-Value Coding](https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/KeyValueCoding.html). The dedicated method to get a value from a dictionary is `objectForKey:` or key subscripting (`event["port1"]`) – vadian Dec 02 '16 at 09:40
1

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"];
CodeChanger
  • 7,953
  • 5
  • 49
  • 80
  • But it get's this error `-[__NSSingleObjectArrayI objectForKey:]: unrecognized selector sent to instance` – Jongers Dec 02 '16 at 09:26
  • Check my updated answer i guess you are getting Array in `json` not NSDictionary. – CodeChanger Dec 02 '16 at 09:32
  • Nope, because it becomes (null) after i made json[0] but it did return NSString. I have to make adjustments. – Jongers Dec 02 '16 at 09:45
  • means you didn't get `_strPart1` value ? – CodeChanger Dec 02 '16 at 09:48
  • It gets error when I try to directly get row [0] from `[json[0] objectForKey:@"portItems"];` because json is NSDictionary that's why I made this base on your answer with row [0] `id event = [json valueForKey:@"portitems"];` `_strPart1 = [event[0] valueForKey:@"port1"];` – Jongers Dec 02 '16 at 09:56
  • Thanks & appreciate your efforts. – CodeChanger Dec 02 '16 at 09:59
1
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"];                                                                              
Siva Sankar
  • 543
  • 1
  • 4
  • 18