解析具有不同键的JSON字典数组

时间:2019-06-21 15:29:30

标签: ios objective-c json

我正在研究一个返回JSON的API。尽管我知道处理JSON的代码,但是在使用服务器端API时我无法更改它。

iOS客户端能够解析以下JSON:

def TrianglePulse(search_spectrum, period, amplitude):
    triangle = (signal.sawtooth(period * search_spectrum, 0.5) + 1) * (amplitude/2)

使用:

{"comment":[{"response":"Great idea"},{"response":"Not sure I agree"}]}

当我尝试使用以下不同的键解析相似的JSON时,它不会解析。不幸的是,我看不到哪里出了问题,只是看不到问题。

if (![[json objectForKey:@"comment"] isKindOfClass:[NSNull class]]) {
    NSMutableArray *someComments = [[json objectForKey:@"comment"]mutableCopy];
    if ([someComments count]>=1) {
        NSDictionary *topComment = someComments[0];
        comment = [topComment valueForKey:@"response"];
        if (comment.length>=1) {
//display
        }
    }
}

我无法更改iOS代码,只能更改API和JSON。有人可以解释为什么{"comment":[{"value":"Great idea","response":"Great idea"}]} 的存在使事情一团糟吗?该值字段由应用程序中的其他代码调用,并且要摆脱它,我必须重写一堆查询-不是不可能,但是我希望不要这样做。

0 个答案:

没有答案
相关问题