iOS Development--Error summary & common errors and warnings in development summary (19)

Source: Internet
Author: User

Summary of common errors and warnings in development (19)

]nsjsonserialization Parsing error is neither a dictionary nor an array

Recently in the implementation of JSON parsing encountered a very classic error, looking for a long time did not find a good, method, after looking at the Internet, found that their brains were squeezed by the door!

I managed to get the data from my server. And get it after my data is sent to the function to analyze;

1- (void) Readin: (Nsmutabledata *) s {2NSLog (@"Reading in the following:");3NSString * Prints =[[NSString alloc] initwithdata:s encoding:nsutf8stringencoding];4NSLog (@"%@", prints);5 6Nserror *error =Nil;7NSData *jsondata =[[NSData alloc] initwithdata:s];8 9     if(jsondata) {Ten  One         IDJsonobjects = [Nsjsonserialization jsonobjectwithdata:jsondata options:nsjsonreadingmutablecontainers error:&ERROR]; A  -         if([Jsonobjects iskindofclass: [Nsarrayclass]]) -NSLog (@"Yes we got an Array"); the         Else if([Jsonobjects iskindofclass: [nsdictionaryclass]]) -NSLog (@"Yes we got an dictionary"); -         Else -NSLog (@"neither array nor dictionary!"); +  -  +  A         if(Error) { atNSLog (@"error is%@", [Error localizeddescription]); -             return; -         } -  -Nsarray *keys =[Jsonobjects AllKeys]; -          for(NSString *keyinchkeys) { inNSLog (@"%@ is%@", Key, [jsonobjects Objectforkey:key]); -         } to  +}Else { -  the         //Handle Error *     }  $}

Now what I'm printing on the console is:

    • 2012-08-17 13:59:57.667 TaraftarlikOyunu[1157:c07] Reading in the following:2012-08-17 13:59:57.667 TaraftarlikOyunu[1157:c07] {"uID":"5878341","tm":"fb","hh":122,"pt":75,"coin":500,"ll":1,"qlevel":1,"coect":true,"potWeekly":{"pts":75,"intval":604800000},"acent":{"chamunt":0},"mes":[]}2012-08-17 13:59:57.668 TaraftarlikOyunu[1157:c07] neither array nor dictionary!2012-08-17 13:59:57.670 TaraftarlikOyunu[1157:c07] error is The operation couldn’t be completed. (Cocoa error 3840.)

Looks like the legal JSON object to me. Where did I do it wrong?

I got the data from Nsstream; Get the data from the server and here is my code to get it:

1  Casensstreameventhasbytesavailable: {2             if(Stream = =InputStream) {3NSLog (@"InputStream is ready.");4 5uint8_t buf[1024x768];6UnsignedintLen =0;7 8Len = [InputStream read:buf maxLength:1024x768];9NSLog (@"Length%i", Len);Ten                 if(Len >0) { One  Ansmutabledata* Data=[[nsmutabledata Alloc] Initwithlength:0]; -[Data appendbytes: (Const void*) buf Length:len]; - [self readin:data]; the  -                 } -             } -              Break; +}

Workaround 1:

The problem is that I get the JSON string to be terminated with null at the end, and when I try to deserialize it can not be converted to nsdictionary or Nsarray. Making a little change to the code makes everything perfect. The real code should be like this

1  Casensstreameventhasbytesavailable: {2         if(Stream = =InputStream) {3 4NSLog (@"InputStream is ready.");5 6uint8_t buf[1024x768];7UnsignedintLen =0;8 9Len = [InputStream read:buf maxLength:1024x768];TenNSLog (@"Length%i", Len); One             if(Len >0) { A  -Datum =[[nsmutabledata alloc] Initwithlength:0]; -  the[Datum appendbytes: (Const void*) BUF length:len-1]; -  -  -  +  -Nsdictionary * Jsondict =[nsjsonserialization jsonobjectwithdata:datum options:nsutf8stringencoding Error:nil]; +  ANSLog (@"is valid JSON object%d", [nsjsonserialization isvalidjsonobject:jsondict]); at  -  - [self readin:datum]; -                 } -             } -              Else { inNSLog (@"No buffer!"); -             } to              Break; +             } -  the  *     default: { $NSLog (@"Stream is sending an Event:%i",Event);Panax Notoginseng  -          Break; the     } +}

The only difference from the other is the last byte I threw, which became a valid JSON dictionary. Thank you for being interested in my question.

iOS Development--Error summary & common errors and warnings in development summary (19)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.