4
"{\n \"connections\": {\n \"_total\": 1,\n \"values\": [{\n \"apiStandardProfileRequest\": {\n \"headers\": {\n \"_total\": 1,\n \"values\": [{\n
我無法讀取此字符串格式的屬性。請告訴我如何從這個字符串格式讀取屬性。如何使用c#讀取這個json字符串?
"{\n \"connections\": {\n \"_total\": 1,\n \"values\": [{\n \"apiStandardProfileRequest\": {\n \"headers\": {\n \"_total\": 1,\n \"values\": [{\n
我無法讀取此字符串格式的屬性。請告訴我如何從這個字符串格式讀取屬性。如何使用c#讀取這個json字符串?
使用這種方法也許有用
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;
}
}
此外,僅供參考,這裏是序列化方法:
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"]
});
}
此代碼也工作正常...
檢查:http://stackoverflow.com/questions/7895105/json-deserialize-c-sharp – 2013-02-12 11:08:57
檢查:http://stackoverflow.com/questio NS/12676746 /解析-JSON串式-C-尖銳 – Falaque 2013-02-12 11:09:50