简体   繁体   中英

JSON String Encoding - Confusion

Am facing a really confusing situation while parsing with json.

The Scenario is,

Step 1 : I have two links

     1. http://www.xyz.com/json/getpidl.asp?id=5527446
     2. http://www.xyz.com/json/getpidl.acp?id=5587963
     ****BOTH LINKS ARE WORKING** **

Step 2 : Now am trying to encode the json string from these two links(one by one) with the following code

    NSMutableString *responseString = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSError *error;
SBJSON *parserObject = [[SBJSON alloc] init];

Step 3 : a) Link1 - Response string has the json string b) Link2 - Response string is empty

Step 4 : So, I have changed the code like following,

    NSMutableString *responseString = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];


    if (!(responseString))
    {
    responseString = [[NSMutableString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];
    }
    NSError *error;
SBJSON *parserObject = [[SBJSON alloc] init]; 

Step 5 : Now the problem has been solved. But I can't understand the problem here. Can anyone help me to understand this scenario

It's unclear how this is related to JSON. Your code snippets create SBJSON parser objects, but never use them.

It is also hard to figure out why the initWithData calls are failing without seeing what the data looks like. My guess would be that the data includes byte sequences that are not valid UTF8, so decoding as UTF8 fails, but decoding as ASCII works because the "bad" characters are just treated as unknown single-byte characters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM