1

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

8
  • 4
    In the title you mention there is an exception - which exception? Commented May 25, 2012 at 13:32
  • 1
    According the jsonlint.com that is valid JSON. Does your parser enforce any encoding schemes (like UTF-8) for JSON and are you reading/writing/transmitting with that proper scheme? What is the error text? Commented May 25, 2012 at 13:32
  • Are all members of the "obj" public or you have some protected? Commented May 25, 2012 at 13:33
  • is there any reason your not escaping the quotes here : "message":"Processed Successfully","serviceName":"CreateCache","serviceStatus":"S"} Commented May 25, 2012 at 13:37
  • Error converting value "[{"MenuId":483,"Name":"Nikhil menu","Desc":"test","ASAP":"T","LT":"T{"Mon":"T","Tue":"T","Wed":"T","Thu":"T","Fri":"T","Sat":"T","Sun":"T"},"SpecialOffer":null,"AddOnList":[],"ItemModList":[]}]}]}]" to type 'System.Collections.Generic.List`1[Menu]'. Commented May 25, 2012 at 13:40

3 Answers 3

1

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();
Sign up to request clarification or add additional context in comments.

3 Comments

it is deserialized from List<menu>
JavaScript doesn't support List<T> natively, meaning although it was serialised from a List<T> doesn't necessarily mean it'll de-serialise to a List<T>. Did you try T[]?
Thnx... doing it this way now :)
0

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 ?

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.