如何在 Ios 中解析数组数据中的嵌套 Json 对象

Posted

技术标签:

【中文标题】如何在 Ios 中解析数组数据中的嵌套 Json 对象【英文标题】:How to parse nested Json Object inside Array Data in Ios 【发布时间】:2017-03-25 08:01:49 【问题描述】:

我将 JSON 数据转换为以下格式,我如何将值发布到其中,因为它给了我错误:域:NSCocoaErrorDomain" Json 文本没有以数组或对象开头,并且允许未设置片段的选项!!!请帮助! !!

[
  "No:"1" , "firstname":dhruv ....,
  "No:"2", "firstname" : something i want to post... ]

代码:

Nsstring* URL string = [NStringwithformat:@Myurl?fname=%(value from editext ]

【问题讨论】:

你能显示完整的错误吗 你能展示一下你尝试过的代码吗 你的数据是什么格式的,你是怎么添加的? 你搜索过吗?有数百个相关的问题和答案。 Related 列中甚至还有一些。 @Anbu.Karthikr 我更新了我的代码!!!! 【参考方案1】:
NSArray *arrData = @[@
                          @"No": @"1",
                          @"firstname": @"dhruv",
                        @
                           @"No": @"2",
                           @"firstname": @"something i want to post"
                        ];

NSData *data = [NSJSONSerialization dataWithJSONObject:arrData options:0 error:nil];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"your url here"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];

NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionUploadTask *dataTask = [session uploadTaskWithRequest: request
                                                             fromData:data completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) 
                                                                 //Kindly use below either one  according to your response
                                                                 //If the response is in Dictionary format
                                                                 NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
                                                                 NSLog(@"%@", jsonDict);

                                                                 //If the response is in Array format
                                                                 NSArray *jsonArr = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
                                                                 NSLog(@"%@", jsonArr);
                                                             ];

[dataTask resume];

【讨论】:

以上是关于如何在 Ios 中解析数组数据中的嵌套 Json 对象的主要内容,如果未能解决你的问题,请参考以下文章

在 IOS 中解析嵌套的 JSON 代码

如何解析嵌套的 JSON 对象?

如何解析 json 值的动态变化。带有 2 个嵌套 dic 的字典,然后是数组,然后是 dic

使用 Mantle 在 iOS 中解析嵌套的 json

解析嵌套的 json 问题

Mantle 2.0 无法为嵌套数组中的符号解析 JSON