9

I'm deserializing a JSON string into an object. I can't use a Dictionary<string, string> because the JSON inside is complex. I know about the Dictionary<string, dynamic>, but I'm over the .NET 3.5 framework, so I can't use dynamic.

So I ended up here:

object json = new JavaScriptSerializer().Deserialize<object>("myjson");

But I see no way to access json without reflection. Any tips?

casperOne
  • 73,706
  • 19
  • 184
  • 253
  • I'm not very familiar with this domain so I apologize if this is a silly question... but why can't you use reflection to access `json.html`? – Kiley Naro Nov 07 '11 at 17:22
  • 1
    I can use, but I think if you see yourself using reflection in the middle of something trivial, you are doing something wrong. In this case, I think Im. –  Nov 07 '11 at 17:37

3 Answers3

13

I would use ServiceStack.Text and parse it using JsonObject.Parse.

Then you have a Dictionary of data that is easy to read.

ServiceStack is faster and better than Json.NET.

Programista
  • 1,037
  • 5
  • 15
  • 38
10

This can be done with ServiceStack's JsonSerializer as easily as:

var dictionary = JsonSerializer.DeserializeFromString<Dictionary<string,string>>(myJson);

It's even more concise if you use the Extension methods:

var dictionary = myJson.FromJson<Dictionary<string,string>>();

Otherwise if you prefer you can use the dynamic API:

var jsonObj = JsonObject.Parse(myJson);
var value = jsonObj.Get("key");

Here are a couple of real-world usages showing the different ways you can deserialize a dynamic JSON payload:

As a bonus you'll be using .NET's fastest JSON serializer :)

ServiceStack's Json Serializer is also available to download on NuGet with:

PM> Install-Package ServiceStack.Text
mythz
  • 141,670
  • 29
  • 246
  • 390
  • Hi Demis, I have a couple of questions 1. For parsing large json files is there a streaming Json parse option available? 2. When the Json is embedded inside square bracket [], the parsing fails is there any method available to override it? – Gokul Nov 16 '12 at 20:52
  • 1) No. 2) I don't understand the example, but all the parsing hooks + customization's that are available are on `JsConfig` and `JsConfig` – mythz Nov 16 '12 at 20:56
  • Thank You, Demis. I got the following error "SerializationException: Type definitions should start with a '{', expecting serialized type 'RootObject', got string starting with: ]" when trying to parse the json object present [Link-here](http://pastebin.com/WZTSbYNJ). When I remove the first character in the file [ and the last character ] in the file, then I am able to parse successfully. – Gokul Nov 18 '12 at 06:12
1

JSON.NET is a popular JSON serialization library, it allows you to serialize your typed objects to/from JSON as well as get typed representations of the meta-structure (through the JObject class) for when you don't know the structure of your JSON.

I've found it to be better than the offerings that .NET comes with out-of-the-box for JSON many times over.

casperOne
  • 73,706
  • 19
  • 184
  • 253