PDA

View Full Version : Help needed for parsing JSON string




mrl72
Jan 18, 2011, 11:20 AM
I'm running a Facebook FQL query and getting the results back in the following format. I'm having a heck of a time parsing this. It looks like a bunch of nested arrays and dictionaries but no matter what combination I use I just can't get to the UID level.

Can someone help me with parsing the following? Result is returned as an NSArray (I used the kindofclass method to figure this out).


"fql_result_set" = (
{
"first_name" = John;
"last_name" = Doe;
name = "John Doe";
status = {
message = "Hello";
"status_id" = 12345;
time = 1295311568;
};
uid = 123456789;
},
{
"first_name" = Jane;
"last_name" = Doe;
name = "Jane Doe";
status = {
message = "";
"status_id" = 0;
time = 0;
};
uid = 4567890123;
},
);
name = query1;


Any help appreciated!

Cheers.

Thanks.



chown33
Jan 18, 2011, 12:09 PM
What are you using to parse the JSON data?

If you're getting an NSArray, what class is each object in the array?

mrl72
Jan 18, 2011, 01:02 PM
I am not sure I understand your question. I am using the facebook API iOS SDK to execute the query, the result looks like it's being parsed using SBJSON methods and then dumped into an NSArray object called "result". I have been able to successfully parse previous queries (a single query) just by doing something like:


NSDictionary* user = [result objectAtIndex:y]; //y being [user count]
NSDictionary* status = [[result objectAtIndex:y] objectForKey:@"status"];


The example in my previous post comes from a mutliquery instead of a single query and it looks like the result is nested even more and the code example above no longer works. This is how the result looks with single query result:


(
{
"first_name" = John;
"last_name" = Doe;
name = "John Doe";
status = {
message = "Hello";
"status_id" = 12345;
time = 1295311568;
};
uid = 123456789;
},
{
"first_name" = Jane;
"last_name" = Doe;
name = "Jane Doe";
status = {
message = "";
"status_id" = 0;
time = 0;
};
uid = 4567890123;
},
);

chown33
Jan 18, 2011, 03:29 PM
I'm afraid I misunderstood what you meant by the word "parsing". I think you meant "I'm having a heck of a time understanding this."

Let's dissect the first data you posted. Here it is with indentation added to clarify its structure:
"fql_result_set" = (
{
"first_name" = John;
"last_name" = Doe;
name = "John Doe";
status = {
message = "Hello";
"status_id" = 12345;
time = 1295311568;
};
uid = 123456789;
},
{
"first_name" = Jane;
"last_name" = Doe;
name = "Jane Doe";
status = {
message = "";
"status_id" = 0;
time = 0;
};
uid = 4567890123;
},
);
name = query1;

Referring to the JSON spec [1], the first thing I notice is this data isn't JSON. It looks a lot more like a plist format, or the output produced by NSArray and NSDictionary descriptions. So let's dissect it as that kind of data.

What I notice next is the first item is a name/value pair. Unfortunately, a name/value pair should only occur when it's stored in an NSDictionary. But I see no enclosing { } delimiters representing a dictionary, so I'm going to guess that you haven't posted all the data.

Please post all the data. Also post the code that you used to produce the posted data.


What I see next is that there's a key whose name is "fql_result_set", and whose value is an array. That key has a sibling named "name" whose value is a string "query1".

Digging into the array whose key is "fql_result_set", there are two items between the ( ) delimiters that signify an array. Each item is itself a separate dictionary, with keys and values. The value of each one's "status" key is yet another nested dictionary.

So assuming "fql_result_set" is an actual key in an actual NSDictionary, the first thing you should do is get the object for that key. That object should be an NSArray. If it isn't, then stop and post the code you have at that point.


[1] http://json.org/

mrl72
Jan 19, 2011, 09:38 AM
Thank you!! That worked like a charm! I was ignoring the fact that the key was "fql_result_set" so when I accounted for this I was able to drill down to the lower objects.

Thanks again.