"{\n \"connections\": {\n \"_total\": 1,\n \"values\": [{\n \"apiStandardProfileRequest\": {\n \"headers\": {\n \"_total\": 1,\n \"values\": [{\n
我无法读取这种字符串格式的属性。请告诉我如何从这种字符串格式中读取属性。
"{\n \"connections\": {\n \"_total\": 1,\n \"values\": [{\n \"apiStandardProfileRequest\": {\n \"headers\": {\n \"_total\": 1,\n \"values\": [{\n
我无法读取这种字符串格式的属性。请告诉我如何从这种字符串格式中读取属性。
使用这种方法可能会很有用。
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;
}
}
此外,仅供参考,这里是Serialize方法:
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());
}
}
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"]
});
}
这段代码也可以正常工作...