我正在从服务器获取字符串格式的JSON,并将其保存到文本文件中
然后我正在读取该文本文件并将其返回给服务器,但在那里我无法将其解析回对象。 可能是因为转义序列导致了问题,或者我不知道。
请建议,我正在使用C#和Newtonsoft的 JSON.NET
以下是示例:
从服务器接收的字符串并将其保存到本地 txt 文件
{"data":"[{"MenuId":483,"Name":"Nikhil menu","Desc":"test","ASAP":"T","LT":"T","FO":"T","catList":[{"CatId":5132,"CatName":"Cate00","Desc":"test","P1":{"Id":1,"Name":"SML"},"P2":{"Id":2,"Name":"MED"},"P3":null,"P4":null,"P5":null,"P6":null,"CatType":"Normal","ItemList":[{"Id":38190,"Name":"XXX","Desc":"tesdt","MinQ":1,"MaxQ":99,"MinP":0.0,"MaxP":0.0,"P1":100.0,"P2":200.0,"P3":-99.0,"P4":-99.0,"P5":-99.0,"P6":-99.0,"Img":"","Icon1":null,"Icon2":null,"Icon3":null,"Icon4":null,"OpenOn":{"Mon":"T","Tue":"T","Wed":"T","Thu":"T","Fri":"T","Sat":"T","Sun":"T"},"SpecialOffer":null,"AddOnList":[],"ItemModList":[]}]}]}]","message":"Processed Successfully","serviceName":"CreateCache","serviceStatus":"S"}
从服务器读取相同的本地文本文件后的字符串
{"data":"[{"MenuId":483,"Name":"Nikhil menu","Desc":"test","ASAP":"T","LT":"T","FO":"T","catList":[{"CatId":5132,"CatName":"Cate00","Desc":"test","P1":{"Id":1,"Name":"SML"},"P2":{"Id":2,"Name":"MED"},"P3":null,"P4":null,"P5":null,"P6":null,"CatType":"Normal","ItemList":[{"Id":38190,"Name":"XXX","Desc":"tesdt","MinQ":1,"MaxQ":99,"MinP":0.0,"MaxP":0.0,"P1":100.0,"P2":200.0,"P3":-99.0,"P4":-99.0,"P5":-99.0,"P6":-99.0,"Img":"","Icon1":null,"Icon2":null,"Icon3":null,"Icon4":null,"OpenOn":{"Mon":"T","Tue":"T","Wed":"T","Thu":"T","Fri":"T","Sat":"T","Sun":"T"},"SpecialOffer":null,"AddOnList":[],"ItemModList":[]}]}]}]","message":"Processed Successfully","serviceName":"CreateCache","serviceStatus":"S"}
在将其添加到另一个类和我用来将其再次发送到服务器的另一个类的对象后得到的字符串,我在服务器上得到这个字符串
{"data":"[{"MenuId":483,"Name":"Nikhil menu","Desc":"test","ASAP":"T","LT":"T","FO":"T","catList":[{"CatId":5132,"CatName":"Cate00","Desc":"test","P1":{"Id":1,"Name":"SML"},"P2":{"Id":2,"Name":"MED"},"P3":null,"P4":null,"P5":null,"P6":null,"CatType":"Normal","ItemList":[{"Id":38190,"Name":"XXX","Desc":"tesdt","MinQ":1,"MaxQ":99,"MinP":0.0,"MaxP":0.0,"P1":100.0,"P2":200.0,"P3":-99.0,"P4":-99.0,"P5":-99.0,"P6":-99.0,"Img":"","Icon1":null,"Icon2":null,"Icon3":null,"Icon4":null,"OpenOn":{"Mon":"T","Tue":"T","Wed":"T","Thu":"T","Fri":"T","Sat":"T","Sun":"T"},"SpecialOffer":null,"AddOnList":[],"ItemModList":[]}]}]}]","message":"Processed Successfully","serviceName":"CreateCache","serviceStatus":"S"}
我无法将此字符串文件解析回列表
我试过了
JObject jObject = JObject.Parse(obj.cacheInfo.cData);
JToken jT = jObject["data"];
List<Menu> lMenu = JsonConvert.DeserializeObject<List<Menu>>(jT.ToString());
JObject jObject = JObject.Parse(obj.cacheInfo.cData);
JObject jObject = JObject.Parse(jObject["data"].ToString());
任何帮助都可以,谢谢
我可能是错的,但是您是否尝试过接受Menu
集合作为数组?我不确定 Json.NET 是否会自动将 JavaScript T
数组转换为List<T>
。试试这个:
List<Menu> lMenu = JsonConvert.DeserializeObject<Menu[]>(jT.ToString()).ToList();
确保属性是公共的。确保反序列化的类具有默认构造函数。类中的数组 - 它们是如何实现的?是的,哪个例外?
问题出在服务器上 JSON.NET 库的版本上。在一台服务器上是 3.5,另一台是 4.5。感谢大家的支持。