解析JSON字符串时遇到意外的字符
任何想法这个JSON字符串可能是错误的?解析JSON字符串时遇到意外的字符
"\"iccid\":\"8500000000000000005\",\"msisdn\":\"4485000000005\",\"comment\":null,\"lastSessionStart\":1480120318000,\"lastSessionEnd\":1480120456000,\"simStatus\":\"Live\",\"activated\":true,\"enabled\":true,\"connected\":false,\"usage\":73728}"
我试图通过它反向转义:
json = json.Replace("\\\"", "");
但我仍然收到此错误:
Unexpected character encountered while parsing value: 1. Path 'lastSessionStart', line 1, position 91.
JSONLint.com说,这是一个有效的JSON格式。
如果这是您的实际JSON字符串,它看起来像是双序列化的。要获取数据,需要反序列化两次:一旦获得未转义的JSON,并且一次获取实际数据。
使用json = json.Replace("\\\"", "");
试图忽略字符串将无法正常工作 - 这实际上会除了反斜杠之外删除所有引号,导致JSON无效。
尝试这样的替代,其中json
是你的问题的字符串:
string unescapedJson = JsonConvert.DeserializeObject<string>(json);
Data data = JsonConvert.DeserializeObject<Data>(unescapedJson);
和Data
是以下类:
public class Data
{
public string iccid { get; set; }
public string msisdn { get; set; }
public string comment { get; set; }
public long lastSessionStart { get; set; }
public long lastSessionEnd { get; set; }
public string simStatus { get; set; }
public bool activated { get; set; }
public bool enabled { get; set; }
public bool connected { get; set; }
public int usage { get; set; }
}
嗨即时通讯仍然得到同样的错误,我做了你所建议的更改 – Nevi
您需要删除此行:'json = json.Replace (“\\\”“”,“”);' –
“{ “ICCID” “8500000000000000005”, “MSISDN”: “4485000000005”, “评论”:空, “lastSessionStart”:1480120318000, “lastSessionEnd”:1480120456000, “simStatus”: “现场”, “激活”:TR呃,“启用”:真正的,“连接”:假,“用法”:73728}' –