NSString to JSON string

风流意气都作罢 提交于 2019-12-22 11:16:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!