Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read this json string using c#? [duplicate]

Tags:

json

c#

"{\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.

like image 729
sainath sagar Avatar asked Aug 12 '26 13:08

sainath sagar


1 Answers

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());
    }
}