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)?

foson
10.2k2 gold badges37 silver badges53 bronze badges
asked Feb 16, 2012 at 20:51
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"]
 }); 
}
answered Mar 1, 2012 at 2:47

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
answered Feb 16, 2012 at 21:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.