主要就是结构的问题 有结构了 都好说了..给你一点建议 你自己看下..http://ip.taobao.com/service/getIpInfo.php?ip=222.333.44.55请求下这个API返回一个结构 {"code":0,"data":{"country":"\u4e2d\u56fd","country_id":"CN","area":"\u4e1c\u5317","area_id":"200000","region":"\u8fbd\u5b81\u7701","region_id":"210000","city":"\u6c88\u9633\u5e02","city_id":"210100","county":"","county_id":"-1","isp":"\u94c1\u901a","isp_id":"100020","ip":"222.33.44.55"}} 那么我的实体类会这样定义.. [Serializable] public class AddressInfo { public int code { get; set; } public data data { get; set; } } [Serializable] public class data { public string country { get; set; } public string country_id { get; set; } public string area { get; set; } public string area_id { get; set; } /// <summary> /// 省份 /// </summary> public string region { get; set; } public string region_id { get; set; } /// <summary> /// 市 /// </summary> public string city { get; set; } public string city_id { get; set; } public string county { get; set; } public string county_id { get; set; } public string isp { get; set; } public string isp_id { get; set; } public string ip { get; set; } }实际上 你的结构 无非就分层了 类中类.. 下面代码是返回实体的..var m=Newtonsoft.Json.JsonConvert.DeserializeObject<AddressInfo>(addr); 至于你那个东西 仅仅是一个实体对象,根本不涉及到List
针对你的这个实体 public class xxoo { public int code{get;set;} public result res{get;set} } public class result { public int id{get;set;} public int name{get;set;} public text t{get;set;} } public class text { //这里面我也不知道怎么写了... }
public class Date { public string date1 { get; set; }//虽然看似日期,但却没用标准日期格式,因此视为字符串 public string date2 { get; set; }//但是字符串又没有引号,因此json转换必定报错,姑且这么定义 } public class Idrate { public int rate1 { get; set; } public int rete2 { get; set; } public Date[] date { get; set; } } public class Text { public string idname { get; set; } public string idtext { get; set; } public Idrate[] idrate { get; set; } } public class Result { public int id { get; set; } public int name { get; set; } public Text[] text { get; set; } } public class SampleResponse1 { public int code { get; set; } public Result result { get; set; } }
[Serializable]和Json没有半点关系,那个只是系统自带的二进制序列化才需要,对于第三方Json序列化类来说根本就用不上。JavaScriptSerializer json = new JavaScriptSerializer(); List<Test> list = json.Deserialize(Json字符串, typeof(List<Test>)) as List<Test>;这样做???
我现在这串Json数据 我看分了好几级, 怎么解析啊 .. 最主要没有拆分的头绪, 因为一个子级下可能有N多数据, 然后在出现新子级..\
JsonConvert.DeserializeObject<T>(JsonString);
{"code":0,"data":{"country":"\u4e2d\u56fd","country_id":"CN","area":"\u4e1c\u5317","area_id":"200000","region":"\u8fbd\u5b81\u7701","region_id":"210000","city":"\u6c88\u9633\u5e02","city_id":"210100","county":"","county_id":"-1","isp":"\u94c1\u901a","isp_id":"100020","ip":"222.33.44.55"}}
那么我的实体类会这样定义..
[Serializable]
public class AddressInfo
{
public int code { get; set; }
public data data { get; set; }
}
[Serializable]
public class data
{
public string country { get; set; }
public string country_id { get; set; }
public string area { get; set; }
public string area_id { get; set; }
/// <summary>
/// 省份
/// </summary>
public string region { get; set; }
public string region_id { get; set; }
/// <summary>
/// 市
/// </summary>
public string city { get; set; }
public string city_id { get; set; }
public string county { get; set; }
public string county_id { get; set; }
public string isp { get; set; }
public string isp_id { get; set; }
public string ip { get; set; }
}实际上 你的结构 无非就分层了 类中类..
下面代码是返回实体的..var m=Newtonsoft.Json.JsonConvert.DeserializeObject<AddressInfo>(addr);
至于你那个东西 仅仅是一个实体对象,根本不涉及到List
针对你的这个实体 public class xxoo
{
public int code{get;set;}
public result res{get;set}
}
public class result
{
public int id{get;set;}
public int name{get;set;}
public text t{get;set;}
}
public class text
{
//这里面我也不知道怎么写了...
}
{
public string date1 { get; set; }//虽然看似日期,但却没用标准日期格式,因此视为字符串 public string date2 { get; set; }//但是字符串又没有引号,因此json转换必定报错,姑且这么定义
}
public class Idrate
{
public int rate1 { get; set; } public int rete2 { get; set; } public Date[] date { get; set; }
}
public class Text
{
public string idname { get; set; } public string idtext { get; set; } public Idrate[] idrate { get; set; }
}
public class Result
{
public int id { get; set; } public int name { get; set; } public Text[] text { get; set; }
}
public class SampleResponse1
{
public int code { get; set; } public Result result { get; set; }
}
不行啊 报异常啊, 放不进list<>里啊无法将类型为“System.Collections.Generic.Dictionary`2[System.String,System.Object]”的对象强制转换为类型“System.Collections.Generic.List`1[PeopleTravel.WebForm2+Test]”。
不行啊 报异常啊, 放不进list<>里啊无法将类型为“System.Collections.Generic.Dictionary`2[System.String,System.Object]”的对象强制转换为类型“System.Collections.Generic.List`1[PeopleTravel.WebForm2+Test]”。实体类头部必须加 [Serializable]
这是 序列化的前提!然后你按照json格式定义实体类。
List<Test> list = json.Deserialize(Json字符串, typeof(List<Test>)) as List<Test>;这样做???
我想问一下 把这个绑在REPEATER上 数据源为
var m = Newtonsoft.Json.JsonConvert.DeserializeObject<Test>(UrlFilesDetail(url));
rep.DataSource=m.Test.Idrate;
页面绑值用的Eval("对象名");
可是我想绑定Idrate的下一级需要怎么操作? 这里并没有告诉过我下级的数量, 如果用Idrate[].Date 里的东西 首先不知道[]里该以什么作为下标, 而且感觉上数据绑定也有问题.
然后 json数据是 {{"ID":1},{"ID",2}} 你的是嵌套的,也就是说你的数据结构是动态的,没法确定。
试一试,先反序列化为object 或者 dymic。然后反射,拿出属性名,构造出动态对象。试试。