3
"{\n  \"connections\": {\n    \"_total\": 1,\n    \"values\": [{\n \"apiStandardProfileRequest\": {\n        \"headers\": {\n \"_total\": 1,\n          \"values\": [{\n   

I am unable to read attributes of this string format. Please suggest me how to read attributes from this string format.

2

2 Answers 2

2

use this method maybe useful

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

Also, just for reference, here is the Serialize method :

public static string Serialize<T>(T obj)
{
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
    using (MemoryStream ms = new MemoryStream())
    {
        serializer.WriteObject(ms, obj);
        return Encoding.Default.GetString(ms.ToArray());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0
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"]
});             

}

This code also working fine ...

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.