2

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

1
  • that is no valid json. Commented Dec 18, 2013 at 11:48

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

my JSON is like Collection ={ contact = { after which i get an extra { to go ahead i cannot provide object forkey to go ahead to attribute list i cannot go ahead and read attribute list as i get stuck before that
JSON parser returns you collection only. And to be specific, object of NSDictionary.
@user3115051: tell me which object/value you want to extract?
Hi Anoop my JSON is like Collection ={ contact = { after which i get an extra { to go ahead i cannot provide object forkey to go ahead to attribute list i cannot go ahead and read attribute list as i get stuck before that
as with the answer posted, you need to iterate to get name and values as it is an array.
1

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"];
    }
}

Comments

0

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
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.