我正在尝试反序列化JSON数据,下面是json数据的结构
[{"empcode":"e123","joining_date":"2017-10-31T00:00:00","pfno":"pf232323","Rating":"A","emp_Type":"full","Project_name":"abcd 123bcc "C""}]
Project_name由json
中的双引号字符组成我使用newtonsoft.json进行序列化和反序列化JSON数据
当我将JSON数据反序列化为数据表时,在解析值时会出现错误,遇到意外字符:C。
public static datatable getDataFromServer(string url)
{
HttpWebRequest req = null;
HttpWebResponse res = null;
//string url = url1 + date;
//Console.WriteLine(url);
req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";
req.ContentType = "application/json; charset=utf-8";
// req = AddProxy(req);
res = (HttpWebResponse)req.GetResponse();
Stream responseStream = res.GetResponseStream();
var streamReader = new StreamReader(responseStream);
string txt = streamReader.ReadToEnd();
txt = txt.Replace("\\", "").Replace("/", "");
// txt = txt.Replace("\\", "");
// txt = txt.Replace(@"\", "");
txt = txt.Remove(0, 1);
txt = txt.Remove(txt.Length - 1, 1);
DataTable ds = GetDeserializedFrmJson(txt);
streamReader.Close();
streamReader.Dispose();
responseStream.Close();
responseStream.Dispose();
return ds;
}
public static DataTable GetDeserializedFrmJson(string data)
{
DataTable dt = (DataTable)JsonConvert.DeserializeObject(data, (typeof(DataTable)));
return dt;
}
答案 0 :(得分:1)
我正在尝试反序列化JSON数据,下面是json数据的结构
不,不是。这不是有效的JSON。错误正是它所说的那样,有些引号没有正确转义,所以你有一个悬空的“C”,它是无效的JSON。
我不知道该告诉你什么。您的代码可能没问题(没有查看),您需要修复数据以使其成为有效的JSON。