How to read this json line using c #?

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

I can not read the attributes of this string format. Please invite me to read the attributes from this string format.

+4
source share
2 answers

using this method is probably 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, 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()); } } 
+2
source
 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 works great ...

0
source

All Articles