开发者社区> 问答> 正文

.net 解析复杂的json数据

{
 "queryorders_result": "{\"data\":[{\"SplitType\":3,\"cosPrice\":0,\"details\":[{\"firstLevle\":1316,\"productId\":1294340891,\"quantity\":1,\"secondLevel\":1381,\"thirdLevle\":1391,\"totalPrice\":138,\"wareId\":1715748194,\"ygCosFee\":110.4}],\"orderId\":13966560746,\"orderTime\":\"2016-04-01 15:21:59\",\"parentId\":0,\"popId\":157021,\"sourceEmt\":1,\"totalMoney\":138,\"yn\":0},{\"SplitType\":3,\"cosPrice\":0,\"details\":[{\"firstLevle\":1316,\"productId\":1294340891,\"quantity\":1,\"secondLevel\":1381,\"thirdLevle\":1391,\"totalPrice\":138,\"wareId\":1715748194,\"ygCosFee\":110.4}],\"orderId\":13946016040,\"orderTime\":\"2016-04-01 15:20:32\",\"parentId\":0,\"popId\":157021,\"sourceEmt\":1,\"totalMoney\":138,\"yn\":0},{\"SplitType\":3,\"cosPrice\":0,\"details\":[{\"firstLevle\":6144,\"productId\":1003881889,\"quantity\":1,\"secondLevel\":6182,\"thirdLevle\":6192,\"totalPrice\":29,\"wareId\":1014999404,\"ygCosFee\":23.2}],\"orderId\":13966538562,\"orderTime\":\"2016-04-01 15:18:26\",\"parentId\":0,\"popId\":31689,\"sourceEmt\":1,\"totalMoney\":29,\"yn\":0}]}"
 }

想要解析出 details

各位大神帮忙啦

展开
收起
杨冬芳 2016-07-07 16:06:41 2407 0
1 条回答
写回答
取消 提交回答
  • 你先定义类,然后使用Gson的fromJson方法转换成类。 下面是我的代码: public class showapi_res_body { private cityInfo cityInfo = new cityInfo();  private f1 f1 = new f1();  @Override public String toString() { return "showapi_res_body [cityInfo=" + cityInfo.toString() + ", f1=" + f1.toString() + "]"; }  }  public class day_weather_pic { private int night_air_temperature = 19; private String night_weather = "多云"; @Override public String toString() { return "day_weather_pic [night_air_temperature=" + night_air_temperature + ", night_weather=" + night_weather + "]"; }  }  public class f1 { private String day="20150906"; private int day_air_temperature=3; private String day_weather= "晴"; private day_weather_pic day_weather_pic = new day_weather_pic(); @Override public String toString() { return "f1 [day=" + day + ", day_air_temperature=" + day_air_temperature + ", day_weather=" + day_weather + ", day_weather_pic=" + day_weather_pic.toString() + "]"; }  }  public class cityInfo {  private String c2 ="lanzhou"; private String c3 ="兰州"; @Override public String toString() { return "cityInfo [c2=" + c2 + ", c3=" + c3 + "]"; }  }  public class showapi { private int showapi_res_code = 0; private String showapi_res_error = ""; private showapi_res_body showapi_res_body = new showapi_res_body(); @Override public String toString() { return "showapi [showapi_res_code=" + showapi_res_code + ", showapi_res_error=" + showapi_res_error + ", showapi_res_body=" + showapi_res_body.toString() + "]"; }  }  public class test { public static void main(String[] args) { showapi anObject = new showapi();  Gson gson = new Gson(); String json = gson.toJson(anObject); System.out.println(json.toString());  showapi a = gson.fromJson(json, showapi.class);  System.out.println(a.toString()); } }  运行后输出的日志:  {"showapi_res_code":0,"showapi_res_error":"","showapi_res_body":{"cityInfo":{"c2":"lanzhou","c3":"兰州"},"f1":{"day":"20150906","day_air_temperature":3,"day_weather":"晴","day_weather_pic":{"night_air_temperature":19,"night_weather":"多云"}}}}  showapi [showapi_res_code=0, showapi_res_error=, showapi_res_body=showapi_res_body [cityInfo=cityInfo [c2=lanzhou, c3=兰州], f1=f1 [day=20150906, day_air_temperature=3, day_weather=晴, day_weather_pic=day_weather_pic [night_air_temperature=19, night_weather=多云]]]]
    

    答案来源网络,供参考,希望对您有帮助

    2019-10-09 11:00:12
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
神龙云服务器产品及技术深度解析 立即下载
弹性创造价值:基于ECS的最佳性价比实践解析 立即下载
又快又稳:阿里云下一代虚拟交换机解析 立即下载

相关镜像