问题
I have a NSString with below format .Currently it is not a valid JSON string .I want to change it as a JSON string and store it in an NSArray.
This is my NSString.
[, {"Agent":" Visitor", "Time":"03:18 AM", "Message":"Msg from : file:///Users/satheesanop/Desktop/something.htmlIP Address : 182.72.139.142Country : IndiaCity : MadrasRegionName : Tamil N?duBrowser : Chrome 22OS : Mac", }, {"Agent":" agent name", "Time":"03:21 AM", "Message":"Thank you for contacting us.How may i help you ?", }, {"Agent":" Visitor", "Time":"03:22 AM", "Message":"Some message.", }]
Any help is appreciated .
回答1:
You need to convert NSString to NSData using dataUsingEncoding: method.
then use the JSONObjectWithData method and add it to array.
NSArray *arrayJson = [NSArray arrayWithObjects:[NSJSONSerialization JSONObjectWithData:data options:0 error:nil],nil];
回答2:
Figured it out ..
Mystr = [Mystr stringByReplacingOccurrencesOfString:@"[," withString:@"["];
Mystr = [Mystr stringByReplacingOccurrencesOfString:@", }" withString:@"}"];
Thanks Everybody.
来源:https://stackoverflow.com/questions/13139483/nsstring-to-json-string