String 尝试将字符串转换为json对象
我有一个以下格式的字符串:String 尝试将字符串转换为json对象,string,jsonobject,String,Jsonobject,我有一个以下格式的字符串: { "payload" : { "name1 " : "value", "Dir" : "<users>/<userid>/<YYYY>/<MM>/<DD>", "file" : "username<userid>.json",
{
"payload" : {
"name1 " : "value",
"Dir" : "<users>/<userid>/<YYYY>/<MM>/<DD>",
"file" : "username<userid>.json",
"userid" : <int>
},
"PK_id" : "xxxxxxxxxxxxxx"
}
但对于目录位置和文件名中使用的反斜杠,我遇到了以下错误:
Exception in thread "main" org.json.JSONException: Expected a ',' or '}'
我需要知道我做错了什么,正确的方法是什么
谢谢
NSString *strJson = @"your json string here";
如果您没有json字符串,那么首先将NSDictionary转换为json字符串
NSData *data = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
请先将主字典转换为json字符串。谢谢,但是使用了org.simple.json库及其工作机制。之前我使用了org.json,现在我使用的是org.simple.json。更改库后,我没有收到错误,谢谢您的帮助!
NSData *data = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];