iOS JSON Parsing Issue quotes and parenthesis

mngoose

macrumors member
Original poster
May 26, 2012
38
0
First of all thanks for the help guys in advance. I have found this forum to be of great help with getting the issues resolved that I am having coming from Android / HTML to IPhone / XCODE programming even if it is just pointing me in the right direction.

I have noticed something weird with the JSON parsing format. Half of the variables have quotes around them and the other half do not. They all have parenthesis around them as well.

Here is my json parsing code in the .m file
Code:
 NSString *strURL = [NSString stringWithFormat:@"http://10.4.4.11/ticket4/android5.php?Fone=%@&pincode=%@&confirm=%@&edit=Register", PhoneText, PinText,ConfirmText ];
    
    NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString : strURL]];
    
    NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
    
    NSData * datajson = [strResult dataUsingEncoding: NSUTF8StringEncoding];
    
    id my2ConvertedJSONData = [NSJSONSerialization JSONObjectWithData : datajson options: NSJSONReadingMutableContainers error:&localError];
    
    if ([my2ConvertedJSONData isKindOfClass:[NSDictionary class]]) {
        NSLog(@"DICTIONARY");
        NSLog(@"dict3: %@", my2ConvertedJSONData);
    }
    
    if ([my2ConvertedJSONData isKindOfClass:[NSArray class]]) 
    {
        NSArray * anArray = my2ConvertedJSONData;
        NSLog(@"ARRAY count: %i", [anArray count]);
        if ([[anArray objectAtIndex: 0] isKindOfClass:[NSDictionary class]]) NSLog(@"Yep, dictionary inside of an array!");
        NSLog(@"Array dict index 0: %@", [anArray objectAtIndex: 0]);
        location =      [my2ConvertedJSONData valueForKey:(NSString *)@"location"];
        firstname =     [my2ConvertedJSONData valueForKey:@"firstname"];
and the results are

Code:
2012-06-11 22:27:58.867 tickets25[4846:f803] firstname = (
    John
)

and

2012-06-11 22:27:58.870 tickets25[4846:f803] location =  (
    "Las Vegas, NV"
)
If I can get rid of the parenthesis and the quotes it would make me a lot happier. It converts half my JSON data string that way and I can't figure out what its doing.

Thanks for all the help guys

John
 

jnoxx

macrumors 65816
Dec 29, 2010
1,343
0
Aartselaar // Antwerp // Belgium
You think wrong about the parantheses and the quotes. This is basic JSON format, you can convert this with the JSONKit/SBJSon whatever to a Dictionary. And then you can ask the value/object ForKey on that dictionary, and it wlil just give a clean NSString, Quotes just mean it's a string, and the '()' is normal JSON for "having data"
 
Comment

dejo

Moderator
Staff member
Sep 2, 2004
15,981
450
The Centennial State
and the results are

Code:
2012-06-11 22:27:58.867 tickets25[4846:f803] firstname = (
    John
)

and

2012-06-11 22:27:58.870 tickets25[4846:f803] location =  (
    "Las Vegas, NV"
)
The code you provided doesn't show how this output is generated (but does show other NSLog calls). When you provide NSLog output, you should also provide the code that generated it.

P.S.
mngoose said:
Code:
    if ([my2ConvertedJSONData isKindOfClass:[NSArray class]]) 
    {
        ...
        location =      [my2ConvertedJSONData valueForKey:(NSString *)@"location"];
        firstname =     [my2ConvertedJSONData valueForKey:@"firstname"];
You sure you want to be calling valueForKey on my2ConvertedJSONData, which you just confirmed is an NSArray with your conditional statement?
 
Comment

mngoose

macrumors member
Original poster
May 26, 2012
38
0
Dejo,

What would you suggest as an alternative for ValueForKey statements ?

what is weird to me is that the json data does not have the quotes when I parse it with my android app nor does it mix my variables with quotes and no quotes. I understand what JSON data does (I did the programming on the php file that produces the JSON array from my webserver).

here is the full json parsing code
Code:
NSString *strURL = [NSString stringWithFormat:@"http://x.x.x.x/apps/apple.php?Fone=%@&pincode=%@&confirm=%@&edit=Register", PhoneText, PinText,ConfirmText ];
    
    NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString : strURL]];
    
    NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
    
    NSData * datajson = [strResult dataUsingEncoding: NSUTF8StringEncoding];
    
    id my2ConvertedJSONData = [NSJSONSerialization JSONObjectWithData : datajson options: NSJSONReadingMutableContainers error:&localError];
    
    if ([my2ConvertedJSONData isKindOfClass:[NSDictionary class]]) {
        NSLog(@"DICTIONARY");
        NSLog(@"dict3: %@", my2ConvertedJSONData);
    }
    
    if ([my2ConvertedJSONData isKindOfClass:[NSArray class]]) 
    {
        NSArray * anArray = my2ConvertedJSONData;
        NSLog(@"ARRAY count: %i", [anArray count]);
        if ([[anArray objectAtIndex: 0] isKindOfClass:[NSDictionary class]]) NSLog(@"Yep, dictionary inside of an array!");
        NSLog(@"Array dict index 0: %@", [anArray objectAtIndex: 0]);
//        location =      [my2ConvertedJSONData valueForKey:(NSString *)@"location"];
        firstname =     [my2ConvertedJSONData valueForKey:@"firstname"];
        lastname =      [my2ConvertedJSONData valueForKey:@"lastname"];
because I am using several of the variables in another part of the program and the parenthesis screw it up with the text variables and another URL string that has to be built I am working on taking out the parenthesis and the quotes but I keep hitting roadblocks.

Point me in the right direction all mighty MAC God :).

Thanks
John
 
Comment

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
Just a couple hints: there are no parentheses and no quotes in your values and valueForKey isn't doing what you think it's doing.

For starters, you should use objectForKey and not valueForKey, unless you really know the difference. The parentheses and quotes are fake.
 
Comment

mngoose

macrumors member
Original poster
May 26, 2012
38
0
Not doubting you PhoneyDeveloper (although your tag tends to lead to that conclusion :) Just Kidding,

But when I NSLog the entire string with it parsed out in the proper variables (there are more than just two) half of them have quotes and the other half do not. When I am working on rebuilding the next URL for my web views which they are using several variables that are in the JSON string the quotes show up as well as the parenthesis which as you can imagine completely jacks with the URL builds.

I am continuing to work through it to appease the MAC Gods :) (I am a PC / Linux guy normally)

also objectForKey throws an exception. just started working through that error


Thanks
John
 
Comment

chown33

Moderator
Staff member
Aug 9, 2009
8,671
4,942
vertical
Maybe the quotes and parentheses appear because the object you're NSLog'ing isn't an NSString. For example, if the object were some kind of container that outputs quotes when contained objects have spaces, and indicates its containment of objects by showing parentheses.

Just because you think something is a an NSString doesn't mean it is. And casting to NSString* doesn't do a run-time type-check, either. It will only fail if some method is called that the object can't perform.

Do you have any code that actually confirms what the class of the quoted parenthesized object is? Maybe by printing the NSString* returned by the -className method (see NSObject protocol).


also objectForKey throws an exception. just started working through that error
Probably because objectForKey: only works when applied to an NSDictionary.


You posted this code:
Code:
        NSLog(@"ARRAY count: %i", [anArray count]);
        if ([[anArray objectAtIndex: 0] isKindOfClass:[NSDictionary class]]) NSLog(@"Yep, dictionary inside of an array!");
        NSLog(@"Array dict index 0: %@", [anArray objectAtIndex: 0]);
but you didn't post the corresponding output. It would be nice to see that.


You also posted this output:
Code:
2012-06-11 22:27:58.867 tickets25[4846:f803] firstname = (
    John
)
and
Code:
2012-06-11 22:27:58.870 tickets25[4846:f803] location =  (
    "Las Vegas, NV"
)
but didn't post any code that produces that output. This is the code that dejo was asking for when he wrote, "When you provide NSLog output, you should also provide the code that generated it."


EDIT
It would also be a good idea to post the actual JSON text that's being parsed. So we know what the expected data is, instead of having to guess at it from the fragments of code and output.
 
Last edited:
Comment

dejo

Moderator
Staff member
Sep 2, 2004
15,981
450
The Centennial State
What would you suggest as an alternative for ValueForKey statements ?
First, answer me this: what are you expecting calling valueForKey: on your NSArray to achieve?

here is the full json parsing code
Code:
NSString *strURL = [NSString stringWithFormat:@"http://x.x.x.x/apps/apple.php?Fone=%@&pincode=%@&confirm=%@&edit=Register", PhoneText, PinText,ConfirmText ];
    
    NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString : strURL]];
    
    NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
    
    NSData * datajson = [strResult dataUsingEncoding: NSUTF8StringEncoding];
    
    id my2ConvertedJSONData = [NSJSONSerialization JSONObjectWithData : datajson options: NSJSONReadingMutableContainers error:&localError];
    
    if ([my2ConvertedJSONData isKindOfClass:[NSDictionary class]]) {
        NSLog(@"DICTIONARY");
        NSLog(@"dict3: %@", my2ConvertedJSONData);
    }
    
    if ([my2ConvertedJSONData isKindOfClass:[NSArray class]]) 
    {
        NSArray * anArray = my2ConvertedJSONData;
        NSLog(@"ARRAY count: %i", [anArray count]);
        if ([[anArray objectAtIndex: 0] isKindOfClass:[NSDictionary class]]) NSLog(@"Yep, dictionary inside of an array!");
        NSLog(@"Array dict index 0: %@", [anArray objectAtIndex: 0]);
//        location =      [my2ConvertedJSONData valueForKey:(NSString *)@"location"];
        firstname =     [my2ConvertedJSONData valueForKey:@"firstname"];
        lastname =      [my2ConvertedJSONData valueForKey:@"lastname"];
That's not the full code, since your if block isn't even terminated (i.e. it's missing a final } ). Why not provide the entire method that this code snippet is from, as well as how and where any supporting ivars are declared (e.g. PhoneText or localError or firstname)?
 
Comment

mngoose

macrumors member
Original poster
May 26, 2012
38
0
Dejo,

I am straight looking for the value that is parsed out in the JSON String so "phone", "9095551212" with phone being the key and the number being the value for that key. This way i can place the variables where I need them else where in the code to help with other URLS and other items that I have in the program.

I am not at home so I don't have my programming information but all variables are property (non atomic, retain) nsstring * variable in the .h file and then synthesized on the .m file.

the full json string is
Code:
[{"id":"1","firstname":"John","lastname":"Schimanski","phonenumber":"9093768439","company":"195300","pincode":"12345678","email":"jfschimanskijr@gmail.com","confirmationcode":"12345","item":"AA","price":"5","desc":"12","retrieved":"no","sku":"154784596124","salesrep":"john doe","customercompanylogo":"customer1.png","billingcontactname":"jane doe","contactnumber":"9093768439","datetime":"2013-01-01 19:00:00","location":"Las Vegas, Nevada","count":"59"}]


and this is what happens when NSLog the dict index 0

 Array dict index 0: {
    confirmationcode = 12345;
    count = 59;
    datetime = "2013-01-01 19:00:00";
    email = "jfschimanskijr@gmail.com";
    firstname = John;
    id = 1;
    lastname = Schimanski;
    location = "Las Vegas, Nevada";
    phonenumber = 9093768439;
    pincode = 12345678;
    retrieved = no;
    price = 5;
    company = 195300;
    price = 12;
    item = AA;
    sku = 154784596124;
    salesrep = "john doe";
    companylogo = "customer1.png";
    contactname = "9093768439";
    oppcompany= "original1.png";
}


When I place them into their individual variables they end up like what I said before the parenthesis and quotes but the quotes are not on all of them but the parenthesis are.
what I am thinking that I am doing wrong is doubling up the variables somehow and then its quoting them in the valueForKey statement.


John
 
Last edited:
Comment

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
Yikes,

Please look up what [NSArray valueForKey] returns and report it here. Do not pass go until you do that.
 
Comment

mngoose

macrumors member
Original poster
May 26, 2012
38
0
Dejo and Chown33

I managed to solve the problem and get them out of the quotes and the parenthesis. Thanks for pointing me in the right direction.

john
 
Comment

chown33

Moderator
Staff member
Aug 9, 2009
8,671
4,942
vertical
For the benefit of future visitors, please describe what the problem was, and how you solved it.
 
Comment

mngoose

macrumors member
Original poster
May 26, 2012
38
0
Chown

I plan on doing that . It was late and I was frustrated with another problem with the code, so that I have a full record of what the problem was and how to solve it for my own future reference.

John
 
Comment
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.