Reputation: 3485
I'm getting JSON from a server in string format and I am saving it to a text file
Then I'm reading that text file and giving it back to the server but there I'm not able to parse it back to an object. It may be because the escape sequence are causing the problem or I don't know.
Please suggest, I'm using C# and Newtonsoft's JSON.NET
Here are the samples:
String received from server and saving this to local txt file
{"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"}
string after reading the same local text file from the server
{"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"}
string which I get after adding it to and object of another class which I use to send it over again to server and I get this string on server
{"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"}
I am not able to parse this string file back to List
I have tried
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());
any help will do, thanks
Upvotes: 1
Views: 1587
Reputation: 3485
The problem was with the version of the JSON.NET library on the servers. On one server it was 3.5 and other one it was 4.5. Thanx all for all of your support.
Upvotes: 0
Reputation: 3353
Make sure the properties are public. Make sure class you deserializing has default constructor. The arrays in the class - how are they implemented ? And yeah, which exception ?
Upvotes: 0
Reputation: 8280
I could be wrong, but have you tried accepting a collection of Menu
as an array? I'm not sure if Json.NET automatically converts a JavaScript array of T
to a List<T>
. Try this:
List<Menu> lMenu = JsonConvert.DeserializeObject<Menu[]>(jT.ToString()).ToList();
Upvotes: 1