0

I have an external vendor API that returns JSON in this format

{"3":{"id":1,"name":"Scott Foo","age":55},
"59":{"id":2,"name":"Jim Morris","age":62}}

I am trying to deserialize it using the following code

[DataContract]
public class Name
{
    [DataMember]
    public int id { get; set; }

    [DataMember]
    public string name { get; set; }

    [DataMember]
    public int age{ get; set; }
}

Code to deserialize is

List<Name> nameList = Deserialize<List<Name>>(temp); 

where the Deserialize is defined as

public static T Deserialize<T>(string json)
{
    T obj = Activator.CreateInstance<T>();
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
    obj = (T)serializer.ReadObject(ms);
    ms.Close();
    ms.Dispose();
    return obj;
}

The object returned in nameList is of zero count. Any idea how this JSON can be deserialized in .Net (i.e. not using Json.Net or any such third-party dll)?

3
  • what's going on with the "3" and "59" before the id, name, and age attributes that you want? Commented Feb 16, 2012 at 20:56
  • 2
    More likely Deserialize<Dictionary<string, Name>> - since that's not a list/JSON array, it's a JSON object with two properties named "3" and "59". Commented Feb 16, 2012 at 20:57
  • the "3" and the "59" are I think some sort of row ids returned from the third party Commented Feb 16, 2012 at 21:02

2 Answers 2

2

Here is one option.

//using System.Runtime.Serialization.Json;

public static dynamic Deserialize(string content)
{
    return new System.Web.Script.Serialization.JavaScriptSerializer().DeserializeObject(content);
}

var f = Deserialize(json);
List<Name> list = new List<Name>();

foreach(var item1 in (Dictionary<string, object>) f)
{
    Dictionary<string, object> item2 = (Dictionary<string, object>) item1.Value;

    list.Add( new Name(){
        id = (int) item2["id"],
        name = (string) item2["name"],
        age = (int) item2["age"]
    });             

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

Comments

1

Your root object theoretically would be something like this

public class root
{
    public Name 3;
    public Name 59;
}

But 3 and 59 are not valid c# variable/field/property names (they are also dynamic). Therefore, you can not deserialize it to a class.

I see that you don't want to use Json.Net or any such third party dlls but this is how I parsed it using Json.Net

string json = @"{""3"":{""id"":1,""name"":""Scott Foo"",""age"":55},""59"":{""id"":2,""name"":""Jim Morris"",""age"":62}}";

JObject jobj = (JObject)JsonConvert.DeserializeObject(json);

foreach (JProperty user in jobj.Children())
{
    Console.WriteLine(user.Name + "==>" + user.Value["name"]);
}

and the output

3==>Scott Foo
59==>Jim Morris

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.