0

jQuery makes it easy to parseJSON using $.parseJSON()

Example:

 var json = $.parseJSON("[{ name: 'Bill', age: 22, hair: 'Brown' }]");

Then can easily get the data like this:

 var name = json[0].name; var age = json[0].age; ....

Is this something not possible in C#? I saw how JSON.Net reads JSON and exposes it in C#, and it's quite a task to work with it.

Isn't there a way to easily/quickly get JSON just like you can in jQuery?

(and without having to write static code first JSON to Object Class properties to match the JSON object.)

Control Freak
  • 12,965
  • 30
  • 94
  • 145
  • @Remus Rusanu How could this be duplicate when I clearly state *(and without having to write static code first JSON to Object Class properties to match the JSON object.)* ??? – Control Freak Aug 24 '14 at 07:32
  • Json.NET does not require class definitions to deserialize. You can call `JsonConvert.DeserializeObject` and use dynamic to access the properties. That is exactly what is in some of the answers in the duplicate. – Mike Zboray Aug 24 '14 at 07:36
  • I should had choose http://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object instead – Remus Rusanu Aug 24 '14 at 07:40

1 Answers1

0

If the properties of your class match the names in the Json string, you can use :

DataContractJsonSerializer serializer = 
    new DataContractJsonSerializer(typeof(MyClass));
MyClass obj = (MyClass)serializer.ReadObject(responseStream);
racraman
  • 4,988
  • 1
  • 16
  • 16