1

What is the most painless way to deserialize this JSON in C# using JSON.NET?

{
"serNo":{
            "A4":{
                    "vol":[["0","100","0,1"],["0","n","0"]],
                    "fix":"900"
                    },
            "A3":{
                    "vol":[["0","200","0,5"],["0","n","0"]],
                    "fix":"700"
                    }
        }
}

To create a separate class or as collection?

EDIT: There will be multiple "serNo" properties.

5 Answers 5

4

In my opinion, the most painless way to deserialize any JSON is to use the JSON.NET library. See also http://json.codeplex.com.

EDIT: Also see this other question on Stack Overflow: How to deserialize with JSON.NET?

Sign up to request clarification or add additional context in comments.

1 Comment

I'm already using that lib, but I'm not sure how to deserialize this one. It's a bit complex...for me at least.
1

You can use the build in lightweight JavaScriptSerializer. No attributes are required on the classes you want to serialize/deserialize.

It can also handle anonymous types.

Serialization:

var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var objectAsJsonString = serializer.Serialize(objectToSerialize);

Deserialization:

var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
SomeClass deserializedObject = serializer.Deserialize<SomeClass>(objectToDeserialize);

Here is the link to an earlier related question/answer: Error converting JSON to .Net object in asp.net

Comments

1

OK, I solved the problem with JSON.NET by creating this class:

class Counter
{
   public double[][] vol { get; set; }

   public double fix { get; set; }
}

and

deserialized JSON with this expression:

Dictionary<string, Dictionary<string, Counter>> counters = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, Counter>>>(arg.Args[7]);

Where arg.Args[7] is JSON.

Comments

0

Check out the C# section at json.org

Comments

0

See Darin's answer.

I think you easily modify provided source code to match your needs. This is built-in to the .NET framework (System.Web.Script.Serialization), so there are no external dependencies.

2 Comments

I don't require tool, but the way to deserialize given example using JSON.NET.
@dakt Okay, please update your question that you want solve the problem using JSON.NET. Thanks

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.