-2

Hello I am new to iOS development. I am trying to parse a JSON response. Below is the top part of the response:

Table =                     
{
  Rows = {
  results = (
            {
  Cells =    {
      results = (
              {
                Key = Rank;
                Value = "6.251145362854";
                ValueType = "Edm.Double";
                "__metadata" =  {
                                 type = "SP.KeyValue";
                                 };
                                },
              {
                Key = DocId;
                Value = 978473;
                ValueType = "Edm.Int64";
                 "__metadata" =                               
              {
                type = "SP.KeyValue";
              };
              },
              {
              Key = WorkId;
              Value = 978473;
              ValueType = "Edm.Int64";
              "__metadata" =  {
                               type = "SP.KeyValue";
                               };
              },
            {
             Key = Title;
             Value = "Vneea Ready!";
             ValueType = "Edm.String";
             "__metadata" =                                              
             {
                type = "SP.KeyValue";
              };
             },.........................

Now I am using

    NSError *error;
    NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
NSDictionary *results = jsonObject[@"Table"][@"Rows"][@"results"];

So I was able to refine it until here, but then I use

NSDictionary *results = jsonObject[@"Table"][@"Rows"][@"results"][@"Cells"];

When I am going further for Cells and results it is giving me Empty element Error, After referring to this post JSON parsing using NSJSONSerialization in iOS, it seems like "(" means an array in the response, but it is not working for me. Can someone help me, please?

4
  • can u give ur URL, I show the full answer for u Commented May 13, 2014 at 7:15
  • This doesn't look like json. Each result entry has ";" instead of "," and all "=" should be ":" Commented May 13, 2014 at 7:16
  • Please specify which value you have to get from the dictionary after parsing? Commented May 13, 2014 at 7:20
  • Title Value i want to get from the Dictionary. Commented May 13, 2014 at 7:42

1 Answer 1

2

results is an array, not a dictionary, so you cannot access its contents by name. Your JSON doesn't look well formed, though, because Key should be a string ("Title" not Title).

Each element in the results array is a dictionary, so to get the Value that corresponds to Title you can use

NSArray *results=jsonObject[@"Table"][@"Rows"][@"results"];
NSDictionary *result=[results objectAtIndex:0];   // access the first result

for (NSDictionary *result in results) {
   if ([result[@"Key"] isEqualToString:@"Title"]) {
      NSLog(@"The value of Title is %@",result[@"Value"]);
      break;
   }
}
Sign up to request clarification or add additional context in comments.

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.