objective c - Parse string and NSLog array -
Good day!
I tried to parse the string with such a format:
{"code": 200, "serviceID": "53d22b10e46a5", "sender" : 1, "hapier": 0} code:
NSString * strData = [[NSString alloc] initWithData: returndata encoding: NSUTF8StringEncoding]; NSR explorer range = nsmkrange (0, [length of strudata]); NSLog (@ "data from server =% @", strData); NSError * Error = Zero; NSRGular Execution * Rezx = [NSGruler Expression Regular Expression with Partarton: @ "(\ w +)? ([0- 9])" Options: 0 Error: and Addition; NSArray * matches = [regex matchesInString: strData options: 0 category: search range]; NSLog (@ "Content of the content is% @", matches); and the last NSLog is the content of the next arry ("Show & lt; NSSimpleRegularExpressionCheckingResult: 0x71a9e90 & gt; {8, 1} {É Nsregularekspression: 0x71a4370 & gt; (and +)? ([0- 9]) 0x0} "," & lt; Nsimpleregularekspressionchekingresult: 0x71a9d70 & gt; {9, 1} {& lt; Nsregularekspression: 0x71a4370 & gt; (and +)? [0- 9]) 0x0} "," & lt; Nsimpleregularekspressionchekingresult: 0x71a9d90 & gt; {10, 1} {É Nsregularekspression: 0x71a4370 & gt; (and +) ? ([0- 9]) 0x0} "," & lt; Nsimpleregularekspressionchekingresult: 0x71a9c80 & gt (25) ([0- 9]) 0x0} "," & lt; Nsimpleregularekspressionchekingresult: 0x71a3460 & gt; {28, 1} {É Nsregularekspression: 0x71a4370 & gt; (and +)? ([0- 9]) 0x0} "," & lt; Nsimpleregularekspressionchekingresult: 0x71aa380 & gt; {29, 1} {É Nsregularekspression: 0x71a4370 & gt ; (W +)? ([0- 9]) 0x0} "," & lt; NSSimpleRe GularExpressionChe CkingResult: 0x71aa3a0 & gt; {31, 1} {& lt; NSRegularExpression: 0x71a4370 & gt; (W +)? ([0- 9]) 0x0} "," & lt; NSSimpleRegularExpressionCheckingResult: 0x71a9770 & gt; {32, 1} {& lt; NSRegularExpression: 0x71a4370 & gt; (W +)? ([0- 9]) 0x0} "," & lt; NSSimpleRegularExpressionCheckingResult: 0x71a3440 & gt; {34, 1} {& lt; NSRegularExpression: 0x71a4370 & gt; (W +) ([0-9]) 0x0} "," & lt; NSSimpleRegularExpressionCheckingResult: 0x71a9790 & gt; {35, 1} {& lt; NSRegularExpression: 0x71a4370 & gt; (W +) ([0-9]) 0x0} "," & lt; NSSimpleRegularExpressionCheckingResult: 0x71aa320 & gt; {37, 1} {& lt; NSRegularExpression: 0x71a4370 & gt; (W +) ([0-9]) 0x0} "," & lt; NSSimpleRegularExpressionCheckingResult: 0x71aa340 & gt; {49, 1} {& lt; NSRegularExpression: 0x71a4370 & gt; (W +) ([0-9]) 0x0} "," & lt; NSSimpleRegularExpressionCheckingResult: 0x71a8f30 & gt; {61, 1} {& lt; NSRegularExpression: 0x71a4370 & gt; (W +)? ([0- 9]) 0x0} ") But I want to parse the information from the server and save it to the array, please tell me my mistake. Thanks!
Your input string looks like JSON, so a regular expression will work better than:
NSString * json = @ "{\" code \ ": 200, \" serviceID \ ": \" 53d22b10e46a5 \ ", \" sender \ ": 1, \" hasPair \ ": 0} , NSData * jsonData = [json dataUsingEncoding: NSUTF8StringEncoding]; NSError * Error = Zero; id obj = [NSJSONSerialization JSONObjectWithData: jsonData wik P: 0 error: & amp; error], if (obj) {NSAssert ([isKindOfClass obj [NSDictionary NSDumber *] obj; NSNumber * code = dictObj [@ "code"]; NSString * serviceId = dictObj [@ "serviceID" ]; NSNM * Sender = DictObj [@ "Sender"]; NSNumber * Happier = Decotabose [@ "Happier"];} Other {NSGOG (@ "JSON: Failed to parse% @", [Error localization]]; }
Comments
Post a Comment