2017-03-25 55 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