0

I'm new at Json Serialization and Deserialization,

I have

class TestClass
{

    public string Name{get;set;}
    public string Age{get;set;}
    public string Height{get;set;}

}

and have the following serialization function

public void SerializeData()
{

    string jsonData = "{
           {"Name" : "Zeus","Age" : "1825","Height" : "900"},
           {"Name" : "Hera","Age" : "1805","Height" : "200"}
     }";

    var resultList = new List<TestClass>();
    var ser = new JavaScriptSerializer();

    resultList= serializer.Deserialize(jsonData , TestClass)

}

but it doesn't work! keeps throwing "Argument Exception"

Any Help Please?

2
  • please be a bit more explicit about the exception it throws. Commented Jul 16, 2012 at 12:18
  • You can check if your json is valid or not from jsonlint.com Commented Mar 18, 2013 at 10:52

2 Answers 2

3

It looks like your JSON might be incorrect.

A List maps more closely to a JSON array - like:

 [
       {"Name" : "Zeus","Age" : "1825","Height" : "900"},
       {"Name" : "Hera","Age" : "1805","Height" : "200"}
 ]

If you want to use outer curly braces {} then you can serialize to/from a Dictionary<string, TestClass> using JSON like:

 {
       "Zeus" : {"Name" : "Zeus","Age" : "1825","Height" : "900"},
       "Hera" : {"Name" : "Hera","Age" : "1805","Height" : "200"}
 ]
Sign up to request clarification or add additional context in comments.

Comments

1

this does not represent an array:

string jsonData = "{
           {"Name" : "Zeus","Age" : "1825","Height" : "900"},
           {"Name" : "Hera","Age" : "1805","Height" : "200"}
     }";

In order to have an array you should have:

string jsonData = "[
           {"Name" : "Zeus","Age" : "1825","Height" : "900"},
           {"Name" : "Hera","Age" : "1805","Height" : "200"}
     ]";

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.