1. If the background returns a dictionary or an array of data types, use the following method to convert directly:
- {
- Data into a dictionary where Responseobject is the data returned
- nsdictionary *resultdictionary = [nsjsonserialization jsonobjectwithdata:responseobject options: Nsjsonreadingmutablecontainers error: nil];
- NSLog (@ "resultdictionary:%@", resultdictionary);
- //data to array where Responseobject is the data returned
- Nsarray *resultarray = [nsjsonserialization jsonobjectwithdata:responseobject options: Nsjsonreadingmutablecontainers error: nil];
- NSLog (@ "resultarray:%@", resultarray);
- }
2, the above method prints out the dictionary, the array value is NULL, may return with other special strings, you need to convert the data returned to a string:
- {
- NSString *resultstring =[[nsstring alloc] initwithdata:responseobject encoding:nsutf8StringEncoding ];
- }
Print out the string as shown in the following:
Looking at the printed string, the beginning is not preceded by a ' {' (dictionary) or ' (' (array) symbol, but a string of characters, so we need to remove the extra string.
- {
- NSString *changestr = [stringobjects stringbyreplacingoccurrencesofstring:@ "renderreverse&& Renderreverse (" withstring:@" "];
- nsstring *jsonstr = [changestr substringtoindex:tempstr. length-1];
- }
Finally, the parse string is transferred to data and the data to the dictionary:
- {
- Nsmutabledictionary *dictionary = [nsjsonserialization jsonobjectwithdata:[jsonstr DataUsingEncoding:NSUTF 8StringEncoding] options:0 Error: nil];
- }
Finally check the dictionary output to see if it is still empty.
Objective-c method for parsing nsdata data into nsdictionary or Nsarray in the request return