我从服务器获取字符串格式的 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());
任何帮助都可以,谢谢