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?

2
  • it's json Dictionary Commented Dec 2, 2016 at 9:12
  • portitems != portItems. C / Objective-C is case sensitive. Commented Dec 2, 2016 at 9:29

3 Answers 3

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"];
Sign up to request clarification or add additional context in comments.

6 Comments

But it get's this error -[__NSSingleObjectArrayI objectForKey:]: unrecognized selector sent to instance
Check my updated answer i guess you are getting Array in json not NSDictionary.
Nope, because it becomes (null) after i made json[0] but it did return NSString. I have to make adjustments.
means you didn't get _strPart1 value ?
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"];
|
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"];                                                                              

1 Comment

Here response means means result dictionary object.
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"];
}

5 Comments

Check this out.
It was probably objectForKey and valueForKey that made the error.
Don't use valueForKey unless you know what KVC is and you really need its functionality.
@vadian could you direct us to some good documentation about KVC and make us understand what it means. This would be helpful.
Short description: Key-Value Coding. The dedicated method to get a value from a dictionary is objectForKey: or key subscripting (event["port1"])

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.