BackStabber
BackStabber

Reputation: 227

JSon parsing error IOS with empty object key

Hi I have this JSON I'm trying to parse using NSDictionary and trying to get the values as mentioned . but the problem is i do not get a key when i further go into the JSON is there any way to remove this key to go more deep.

NSDictionary *Allinfo = [json objectForKey:@"Collection"];//I go into "Collection"
    NSLog(@"All with response array: %@", Allinfo);
    NSDictionary *datainfo = [Allinfo objectForKey:@"contact"];//i go into "contact"

after which i get

Data : (
    {
    attributeList =         {
        attribute =             (
                            {
                name = "display-name";
                value = "tel:+";
            },
                            {
                name = capabilities;
                value = "TRANSFER";
            },
                            {
                name = relationship;
                value = ACCEPTED;
            }
        );
        resourceURL = "https://example.com";
    };
    contactId = "tel:+";
},
    {
    attributeList =         {
        attribute =             (
                            {
                name = "display-name";
                value = bob;
            },
                            {
                name = capabilities;
                value = "TRANSFER";
            },
                            {
                name = relationship;
                value = ACCEPTED;
            }
        );
        resourceURL = "https://example.com";
    };
    contactId = "tel:+";
}

)

This starts from a { and i do not get a key for the next object so that i can get the values inside the JSOn

Is there any easier way to remove this or any other so that i can get values like name and value and store them in an array.

Thanks in advance

Upvotes: 2

Views: 934

Answers (3)

Code Hunter
Code Hunter

Reputation: 11218

You can use the following method for this

NSError *e = nil;
NSArray *itemArray = [NSJSONSerialization JSONObjectWithData:[response  dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableContainers error: &e];


if (!itemArray) {
    NSLog(@"Error parsing JSON: %@", e);
} else {
    for(NSDictionary *item in itemArray) {
        //Your Data
    }

}

Upvotes: 0

Tapas Pal
Tapas Pal

Reputation: 7207

I am not sure from where you got [Allinfo objectForKey:@"contact"]. Try to parse like below..

NSDictionary *Allinfo = [json objectForKey:@"Collection"];
NSArray *dataArray = [Allinfo objectForKey:@"Data"];

for(NSDictionary *dic in dataArray)
{
    NSDictionary *attributeList=[dic objectForKey:@"attributeList"];
    NSArray *attributeArray=[attributeList objectForKey:@"attribute"];

    NSString *resourceURLString=[attributeList objectForKey:@"resourceURL"];
    NSString *contactIdString=[dic objectForKey:@"contactId"];

    for(NSDictionary *attributeDic in attributeArray)
    {
        NSString *nameString=[attributeDic objectForKey:@"name"];
        NSString *valueString=[attributeDic objectForKey:@"value"];
    }
}

Upvotes: 1

Anoop Vaidya
Anoop Vaidya

Reputation: 46563

You have keys for all.

In fact you seem to get confused on array which is a part of attribute.

The outermost key is Data which contains array.

For each object of the array you have :

The first key is attributeList & contactD.

The value of attribute key is an array of 3 values. Each array contains key value pairs. keys are name & value.

Upvotes: 3

Related Questions