2017-03-25 54 views
-2

我有JSON数据转换为以下格式,我如何张贴值到它,因为它给我错误:域:NSCocoaErrorDomain“Json文本没有开始数组或对象和选项允许片段没有设置!!!请帮助!如何解析在Ios中的数组数据嵌套的Json对象

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

代码:

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

你能显示完整的错误 –

+0

并可以显示乌尔试图代码 –

+0

是什么格式的你的数据,你如何添加它? –

回答

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]; 
+0

thnx男子为您的帮助!有用 !! thnku再 –

+0

好吧,我会做! –

+1

谢谢兄弟:-) – user3182143