0

Deserialize JSON into C# dynamic object?

Following above question, I copy the dynamicJsonDeserilization and trying to use that in my application.

then I try to access the object as

var Data = json.deserilization(jsonstring);

Now, my string is

{"0":{"Name":"C:\\","Type":"Partition","Path":"C:\\"},"1":{"Name":"D:\\","Type":"Partition","Path":"D:\\"},"2":{"Name":"E:\\","Type":"Partition","Path":"E:\\"}}

i.e. I just have an Array on my server which I convert to JSON string and send.

As per code from best answer I should be able to access it as Data.0 but it give "End of Expression expected", Also Data[0] is giving same error. I am not sure how can I use it ? Any help is appreciated. Thanks.

Community
  • 1
  • 1
Sumit Gupta
  • 2,152
  • 4
  • 29
  • 46
  • Have you tried inspecting `Data` by setting a breakpoint at the appropriate position and then holding the mouse pointer over `Data`? This gives you good insight into the object structure of `Data`. – Thorsten Dittmar May 16 '13 at 12:01
  • I did, it show me something this {Name:"ABC",Type:"10"},Name:"Test","Age":"10"}} which looks little bad to me too. – Sumit Gupta May 16 '13 at 12:04
  • Go to http://jsonlint.com/ and feed it your string to verify if it's valid JSON – Alex May 16 '13 at 12:05
  • I don't think so - this doesn't match at all the JSON string you're showing. – Thorsten Dittmar May 16 '13 at 12:06
  • I did that already and JSON is valid ... – Sumit Gupta May 16 '13 at 12:06
  • Okay I change it to original generated JSON String as I see it in browser window. I am fetching remote server Drive Information and hence we get drive name in our string. I should have posted that earlier. – Sumit Gupta May 16 '13 at 12:08

1 Answers1

0

Now, my string is

{"0":{"Name":"C:\","Type":"Partition","Path":"C:\"},"1":{"Name":"D:\","Type":"Partition","Path":"D:\"},"2":{"Name":"E:\","Type":"Partition","Path":"E:\"}}

Your string is indeed not valid JSON due to escaped quotes.

Those C:\ are breaking the parser. You should generate it like this, sending three backslahes:

{"0":{"Name":"C:\\\","Type":"Partition","Path":"C:\\\"} ...

Alex
  • 23,004
  • 4
  • 39
  • 73
  • well I have escape slashes already, but I fix them in my question I didn't know the formatting on stackoverflow before. Sorry. – Sumit Gupta May 16 '13 at 12:11
  • Yes, I noticed that. The fixed string contains **three** backslashes. I'll try and work around the formatting now. – Alex May 16 '13 at 12:12