简体   繁体   English

JSON 字符串编码 - 混淆

[英]JSON String Encoding - Confusion

Am facing a really confusing situation while parsing with json.在使用 json 解析时,我面临着一个非常令人困惑的情况。

The Scenario is,场景是,

Step 1 : I have two links第 1 步:我有两个链接

     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第 2 步:现在我尝试使用以下代码对这两个链接(一个接一个)中的 json 字符串进行编码

    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第 3 步:a) Link1 - 响应字符串具有 json 字符串 b) Link2 - 响应字符串为空

Step 4 : So, I have changed the code like following,第 4 步:所以,我更改了如下代码,

    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.目前尚不清楚这与 JSON 有何关系。 Your code snippets create SBJSON parser objects, but never use them.您的代码片段创建 SBJSON 解析器对象,但从不使用它们。

It is also hard to figure out why the initWithData calls are failing without seeing what the data looks like.也很难弄清楚为什么initWithData调用会在没有看到数据的情况下失败。 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.我的猜测是数据包含无效的 UTF8 字节序列,因此解码为 UTF8 失败,但解码为 ASCII 有效,因为“坏”字符仅被视为未知的单字节字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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