0

I am trying to parse a rather large json file, and I am beginning to have a bit of an issue

An example of what the JSON looks like is this

{
"info":"info",
"products":[
            {
            "name":"HomeScreen",
            "screens":[
                       {
                       "name":"View Details",
                       "id":1,
                       "title":{
                       "name":"Default_Name",
                       "number":"Default_Number"
                       },
                       "widgets":[
                                  {
                                  "id":8,
                                  "splits":[
                                            {
                                            "control":{
                                            "type":1,
                                            "limitLow":0,
                                            "limitHigh":100
                                            },
                                            "texts":[
                                                     {
                                                     "value":"blah",
                                                     "style":{
                                                     "size":10,
                                                     "colour":"#FFFFFF"
                                                     },
                                                     "test1":"asd",
                                                     "test2":"zxc"
                                                     }
                                                     ]
                                            }
                                            ]
                                  },

And so on

I want to be able to parse the texts values and place them in an object. I have the class created, but I am unable to retrieve the exact values. I have tried doing the following

   NSArray *test = [MainJSON valueForKeyPath:@"products.screens.widgets.splits.texts"];

for(NSDictionary *dict in test){

    NSLog(@"%@", [dict valueForKeyPath:@"value"]);
}

It prints out the values but with loads of brackets as well, and I need to be able to get the exact value from the JSON. I also tried doing

 for(int i = 0; i < [test count]; i++){

    for(NSDictionary *dict in [test objectAtIndex:i]){

        NSLog(@"%@", [dict valueForKeyPath:@"value"]);
    }

}

But same problem. Could anyone tell me what I am doing wrong. I have tried soo many different solutions, but nothing seems to be working.

Thanks in advance

EDIT Output for example I gave is

2012-03-21 12:12:18.731 JSONObjects[4467:f803] (
    (
            (
                    (
            "CURRENT BALANCE",
            "[var:UnbilledAmount]"
        )
    ),
            (
                    (
            "THE BILL IS DUE IN",
            "[var:DaysToBill] DAYS"
        )
    ),
            (
                    (
            TEXTS,
            "[var:UsedTexts] of [var:MaxTexts]"
        ),
                    (
            DATA,
            "[var:UsedData] of [var:MaxData]"
        ),
                    (
            TEXTS,
            "[var:UsedMinutes] of [var:MaxMinutes]"
        )
    )
)
)
2012-03-21 12:12:18.732 JSONObjects[4467:f803] (
        (
    ),
        (
    ),
        (
    ),
        (
    ),
        (
    )
)
2012-03-21 12:12:18.732 JSONObjects[4467:f803] (
        (
    )
)
2012-03-21 12:12:18.733 JSONObjects[4467:f803] (
        (
    )
)
2012-03-21 12:12:18.733 JSONObjects[4467:f803] (
        (
    )
)
2012-03-21 12:12:18.734 JSONObjects[4467:f803] (
        (
    )
)
2012-03-21 12:12:18.734 JSONObjects[4467:f803] (
        (
    )
)

As you can see a lot of brackets which is deeply frustrating.

Edit2:

Test is an array of NSDictionary objects though, here is the code

   NSString *filePath = [[NSBundle mainBundle] pathForResource:@"json" ofType:@"json"];  
NSData *myData = [NSData dataWithContentsOfFile:filePath];  

NSString *responseString = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding];

NSDictionary *MainJSON = [responseString JSONValue];
NSArray *test = [MainJSON valueForKeyPath:@"products.screens.widgets.splits.texts"];

Edit:

I have been able to get a single value using this code

 for(NSDictionary *dict in [textArray objectAtIndex:0]){

 NSLog(@"%@", [[[[dict valueForKey:@"value"]objectAtIndex:0]objectAtIndex:0]objectAtIndex:0]);
 }

However this is a bit messy.

12
  • Please add your output to the question - I suspect that the 'loads of brackets as well' are because it's outputting an array instead of the value you expect :) Mar 21, 2012 at 12:00
  • And what happens if you put NSLog(@"%@", [[dict objectForKey:@"value"] class]); to see if the object is a string or something else? Mar 21, 2012 at 12:01
  • Added output to the example I gave. Also I tried that code you gave me, it causes it to crash saying "-[__NSArrayI objectForKey:]: unrecognized selector sent to instance 0x6a37310 "
    – AdamM
    Mar 21, 2012 at 12:13
  • That tells me that test isn't an array of NSDictionary objects, it's an array of NSArrays. Mar 21, 2012 at 13:19
  • Test is an array of NSDictionay objects, though, see edit for the code I have used
    – AdamM
    Mar 21, 2012 at 16:01

1 Answer 1

0

I figured it out in the end. In the products Object class, I didn't add in an NSDictionary object into the class object. As I needed to make a dictionary of the screens. Then loop through that dictionary, find the widgets, then create a dictionary of that array, find the splits, then make a dictionary of that array, and then loop through that and so on until I got to texts. Now it works perfectly! :D

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.