ios 在 Objective C 中解析纯 JSON 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15786109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:00:50  来源:igfitidea点击:

Parsing Plain JSON Array in Objective C

iphoneiosobjective-cxcodejson

提问by Sangram Mohite

I want to parse a plain JSON array like:

我想解析一个普通的 JSON 数组,如:

{
    "ns": [
        [
            "1364987475027",
            "Alert1",
            "001"
        ],
        [
            "1364987475042",
            "Alert2",
            "001"
        ],
        [
            "1364987475058",
            "Alert4",
            "001"
        ]
    ]
}

To get the array in simple arrays of string. I found many posts with JSON dictionary arrays. But in this case the JSON is not having keys for the values. Kindly help.

以简单的字符串数组获取数组。我发现了很多带有 JSON 字典数组的帖子。但在这种情况下,JSON 没有值的键。请帮忙。

回答by Bhavin

Answer :NSJSONSerialization.

答:NSJSONSerialization

NSJSONSerializationclass can be used to convert JSON to Foundation objects and Foundation objects to JSON. In your case, you should use -JSONObjectWithData:options:error:of NSJSONSerializationclass to retrieve a Foundation object from given JSON data.

NSJSONSerialization类可用于将 JSON 转换为 Foundation 对象,将 Foundation 对象转换为 JSON。在你的情况,你应该使用-JSONObjectWithData:options:error:NSJSONSerialization类从给定的JSON数据检索Foundation对象。



Sample Code :

示例代码:

NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSArray *fetchedArr = [json objectForKey:@"ns"];

回答by Lithu T.V

And the description shows what you are looking for

描述显示了您正在寻找的内容

   NSError *error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

    NSArray *fetchedArr = [json objectForKey:@"ns"];

    for (NSArray *arr in fetchedArr) {
        [arr description];
    }

回答by Prasad G

 NSError *error = NULL;
NSData* data = [yourJsonString dataUsingEncoding:NSUTF8StringEncoding];
            NSDictionary* json = [NSJSONSerialization
                                  JSONObjectWithData:data
                                  options:kNilOptions
                                  error:&error];
NSArray *resultArray = [json objectForKey:@"ns"];//resultArray contains array type objects...

I think it will be helpful to you.

我想它会对你有所帮助。

回答by dasdom

The NSJSONSerializationmethod JSONObjectWithData:options:errorcan to that. You will get a dictionary with one value for the key "ns" and the value will be an array of arrays.

NSJSONSerialization方法JSONObjectWithData:options:error可以到。您将获得一个字典,其中包含一个键“ns”的值,该值将是一个数组数组。