111,025
社区成员
发帖
与我相关
我的任务
分享
string jsonStr = @"{""fruits"":{""a"":""orange"",""b"":""banana"",""c"":""apple""},""name"":{""z"":""zhangsan"",""l"":""lisi""}}";
JavaScriptSerializer serializer = new JavaScriptSerializer();
Dictionary<string, object> json = serializer.DeserializeObject(jsonStr) as Dictionary<string, object>;
Dictionary<string, object> obj = json["fruits"] as Dictionary<string, object>;
Console.WriteLine(obj["a"]);
Console.ReadLine();
看看 [DataContract]
public class DanTengObj
{
[DataMember(Order = 1, Name = "fruits")]
public Fruits fruits { get; set; }
[DataMember(Order = 2, Name = "name")]
public Name name { get; set; }
[DataContract]
public class Fruits
{
[DataMember(Order = 1, Name = "a")]
public string a { get; set; }
[DataMember(Order = 2, Name = "b")]
public string b { get; set; }
[DataMember(Order = 3, Name = "c")]
public string c { get; set; }
}
[DataContract]
public class Name
{
[DataMember(Order = 1, Name = "z")]
public string z { get; set; }
[DataMember(Order = 1, Name = "l")]
public string l { get; set; }
}
}
static void DanTengJsonTest()
{
string jsonStr = @"{""fruits"":{""a"":""orange"",""b"":""banana"",""c"":""apple""},""name"":{""z"":""zhangsan"",""l"":""lisi""}}";
DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(DanTengObj));
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonStr)))
{
DanTengObj obj = js.ReadObject(ms) as DanTengObj;
Console.WriteLine(obj.fruits.a);
}
}