-3

I have the following JSON data:

(
    3,
        {
        body = "123";
        date = 1333023644;
        mid = 12;
        "read_state" = 0;
    },
        {
        body = ":)";
        date = 1332968570;
        mid = 4;
        "read_state" = 1;
    },
        {
        body = "1234";
        date = 1331844024;
        mid = 1;
        "read_state" = 1;
    }
)

And I want to get an array of body values with this code:

NSArray *array = [dialogsDictionary objectForKey:@"body"];

But I always get this error:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x6b65a60'

Vlad Z.
  • 3,401
  • 3
  • 32
  • 60
  • I'm not an iphone developer, but, `3` have 3 sub array indexes right? both have their own bodies. Don't you need any loop to fetch the body? because, there are 3 index objects of body. – Hamza Waqas Mar 29 '12 at 16:50
  • Sigh... Peel the onion, one layer at a time. – Hot Licks Mar 29 '12 at 19:18
  • (You have an array containing 4 elements. The first element is the number "3". The remaining 3 elements are dictionaries containing 4 named entries each. To get to a "body" element you must index into the array and extract a dictionary, then ask the dictionary to resurn a "body" element -- which will be a character string in the above example, not another array.) – Hot Licks Mar 29 '12 at 19:22
  • thank you Hot Licks, that make sense – Vlad Z. Mar 29 '12 at 19:44
  • i putting my parsed data to Array and when i try to access to it by objectAtIndex method i have next error: "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x6b44e10'" – Vlad Z. Mar 30 '12 at 10:46
  • That error is due to trying to treat a dictionary as an array. The outermost layer in the above example is an array, but after you extract, say, element 2, what you get back is an NSDictionary. – Hot Licks Apr 01 '12 at 03:40

3 Answers3

1

Consider this JSON string.

  {
   "arrayOfData": [
    {
        "body": "123",
        "date": 1333023644,
        "mid": 12,
        "read_state": 0
    },
    {
        "body": ": )",
        "date": 1332968570,
        "mid": 4,
        "read_state": 1
    },
    {
        "body": "1234",
        "date": 1331844024,
        "mid": 1,
        "read_state": 1
    }
    ]
  }

This is a valid JSON string. You can check validity using http://jsonlint.com/

Suppose you have your JSON data in variable jsonData which is of type NSData. Using SBJSON, you can parse your JSON like

  NSDictionary *jsonDictionary = [jsonData JSONValue];

  NSArray *array = [jsonDictionary objectForKey:@"arrayOfData"];

  NSMutableArray *result = [[NSMutableArray alloc]init];
  for(NSDictionary *dict in array){
      [result addObject:[dict objectForKey:@"body"]];
  }

After this operation all the values for key body would be in result array.

If the '3' was array count, you might have included that value in JSON as,

{
"numberOfElementsInArray": 3,
"arrayOfData": [
    {
        "body": "123",
        "date": 1333023644,
        "mid": 12,
        "read_state": 0
    },
    {
        "body": ": )",
        "date": 1332968570,
        "mid": 4,
        "read_state": 1
    },
    {
        "body": "1234",
        "date": 1331844024,
        "mid": 1,
        "read_state": 1
    }
]
}

See this tutorial about JSON. You must have read this website to understand JSON

Added by Hot Licks

What you fail to understand is that the original JSON was essentially this:

[
    3,
    {
        "body": "123",
        "date": 1333023644,
        "mid": 12,
        "read_state": 0
    },
    {
        "body": ": )",
        "date": 1332968570,
        "mid": 4,
        "read_state": 1
    },
    {
        "body": "1234",
        "date": 1331844024,
        "mid": 1,
        "read_state": 1
    }
]

It was formatted differently because it was an NSLog object dump, not the original JSON source. Perfectly legitimate JSON.

Community
  • 1
  • 1
rakeshNS
  • 4,227
  • 4
  • 28
  • 42
  • I understand tat json data must be like you wrote above ,but site where I send request to get this data returns only Json data like i wrote in my question,strange... – Vlad Z. Mar 30 '12 at 20:19
  • If you are still working on it, report your programmers who writes server side programs that the JSON they sending is in wrong format. If you get valid JSON then only you can get data. – rakeshNS Mar 31 '12 at 04:50
  • The JSON was perfectly legit. – Hot Licks Apr 01 '12 at 03:52
  • If it is legit, then how you can get '3' from JSON? What will be corresponding JSON string for the one you provided? – rakeshNS Apr 01 '12 at 05:54
0

it looks like you are using sudzc

try:

if( [value isKindOfClass:[NSError class]] || [value isKindOfClass:[SoapFault class]] ) {

    NSLog(@"%@", [value description]);
    return;
}

// Verify we're a dictionary
if( ![value isKindOfClass:[NSDictionary class]] ) {

    NSLog(@"ERROR: Response not a dictionary");
    return;
}

NSDictionary* dict = (NSDictionary*)value;
NSDictionary* resp = [dict objectForKey:@"3"];
if( ( resp == nil ) || ![resp isKindOfClass:[NSDictionary class]] ) {

    NSLog(@"ERROR: 3 not a dictionary");
    return;
}
dict = [resp objectForKey:@"body"];
if( ( dict == nil ) || ![dict isKindOfClass:[NSDictionary class]] ) {

    NSLog(@"ERROR: body not a dictionary");
    return;
}
resp = [dict objectForKey:@"date"];
if( ( resp == nil ) || ![resp isKindOfClass:[NSDictionary class]] ) {

    NSLog(@"ERROR: date not a dictionary");
    return;
}
iDev
  • 478
  • 1
  • 3
  • 24
-1

The JSON you provided is not valid.

   (

    {
    body = "123";
    date = 1333023644;
    mid = 12;
    "read_state" = 0;
},
    {
    body = ":)";
    date = 1332968570;
    mid = 4;
    "read_state" = 1;
},
    {
    body = "1234";
    date = 1331844024;
    mid = 1;
    "read_state" = 1;
}
)

If your JSON data is like the given above, you need to write

 for(NSDictionary *dict in Array){
  [result addObject:[dict objectForKey:@"body"]];
 }

The 'array' result will contain the dictionary objects corresponding to 'body' object

rakeshNS
  • 4,227
  • 4
  • 28
  • 42
  • thanks, but i know how to handle JSON that you wrote , i can't figure how to work with JSON like mine – Vlad Z. Mar 29 '12 at 17:15
  • What's not valid about the original JSON?? – Hot Licks Mar 29 '12 at 19:19
  • Just about everything. The "3" can't be just sitting there on its own without a key. The keys need to be quoted. The semicolons don't belong. See http://www.json.org/ and http://json.org/example.html . – EricS Mar 30 '12 at 02:12
  • Structure of JSON might be "key":"value" pair. In the above question, I cant understand what just a '3' alone means.. Instead if it was {3 = 5, ...} it is OK. where, '3' is key and '5' is value. If you have a JSON, you can check the validity using site http://jsonlint.org/ – rakeshNS Mar 30 '12 at 03:56
  • @EricS -- The outermost layer is an array. The "3" is an element of the array. Perfectly legit. (Bear in mind that what's printed is a NSLog dump, not the original JSON.) – Hot Licks Apr 01 '12 at 03:36
  • @rakeshNS -- The structure of the original JSON is an array containing 4 elements, one of which is the number "3", and the other 3 being dictionaries/"objects". Perfectly legit. – Hot Licks Apr 01 '12 at 03:38
  • Ah, I didn't realize that wasn't the source json. NSLog messes with it. We've taken to saving the raw json into files or http posting it to our servers so we can run it through JSONLint and other checkers. You are, of course, right about the stand-alone number. Not sure what I was thinking. – EricS Apr 01 '12 at 23:39