NSString到JSON字符串 [英] NSString to JSON string
本文介绍了NSString到JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 NSString
以下格式。目前它不是一个有效的JSON字符串。我想更改为JSON字符串,并将其存储在 NSArray
。
这是我的 NSString。
代理:访问者,时间:03:18 AM,消息:消息来自:file:///Users/satheesanop/Desktop/something.htmlIP地址:182.72.139.142国家:IndiaCity:MadrasRegionName :Tamil N?duBrowser:Chrome 22OS:Mac,},{Agent:代理名称,时间:03:21 AM,消息:谢谢您与我们联系。 you?,},{Agent:Visitor,Time:03:22 AM,Message:Some message。,}]
任何帮助。
解决方案
使用 dataUsingEncoding:
方法将 NSString
转换为 NSData
p>
然后使用 JSONObjectWithData
方法并将其添加到数组。
NSArray * arrayJson = [NSArray arrayWithObjects:[NSJSONSerialization JSONObjectWithData:data options:0 error:nil],nil];
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 .
解决方案
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];
这篇关于NSString到JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文