怎么获取type为2的后面的所有电力公司name和code

[{"code":"D1_3600","name":"电费缴纳","type":"2","categories":[{"code":"D1_3600_8401","name":"合肥供电公司"},{"code":"D1_3600_0101","name":"肥东供电公司"},{"code":"D1_3600_0102","name":"肥西供电公司"},{"code":"D1_3600_0103","name":"长丰供电公司"},{"code":"D1_3600_8101","name":"安徽电力省公司"}]},{"code":"D3_3600","name":"燃气费缴纳","type":"3","categories":[{"code":"D3_3600_00AH","name":"合肥燃气"}]},{"code":"I1_3600","name":"联通缴费","type":"5","categories":[{"code":"I1_3600_1305","name":"安徽联通"}]},{"code":"J0_3600","name":"彩票投注","type":"34","categories":[{"code":"J0_3600_00AH","name":"安徽福彩"},{"code":"J0_3600_00ZS","name":"招商福彩"},{"code":"J0_3600_00NH","name":"农行福彩"}]},{"code":"D4_3600","name":"水费缴纳","type":"1","categories":[{"code":"D4_3600_00HF","name":"合肥供水"}]},{"code":"I1_3600","name":"移动缴费","type":"4","categories":[{"code":"I1_3600_000A","name":"安徽移动"}]},{"code":"G1_3600","name":"交通罚款","type":"26","categories":[{"code":"G1_3600_3602","name":"交警罚款缴费(安徽非税)"},{"code":"G1_3600_0000","name":"违章代办(第三方处理)"}]},{"code":"I1_3600","name":"有线电视缴费","type":"19","categories":[{"code":"I1_3600_3610","name":"合肥市有线电视"},{"code":"I1_3600_200A","name":"合肥市有线电视"}]},{"code":"S2_3600","name":"医疗充值","type":"47","categories":[{"code":"S2_3600_0000","name":"医疗-健康之路"}]},{"code":"I1_3600","name":"电信手机缴费","type":"15","categories":[{"code":"I1_3600_4550","name":"安徽电信手机缴费(滁州)"},{"code":"I1_3600_4551","name":"安徽电信手机缴费(合肥)"},{"code":"I1_3600_4552","name":"安徽电信手机缴费(蚌埠)"},{"code":"I1_3600_4553","name":"安徽电信手机缴费(芜湖)"},{"code":"I1_3600_4554","name":"安徽电信手机缴费(淮南)"},{"code":"I1_3600_4555","name":"安徽电信手机缴费(马鞍山)"},{"code":"I1_3600_4556","name":"安徽电信手机缴费(安庆)"},{"code":"I1_3600_4557","name":"安徽电信手机缴费(宿州)"},{"code":"I1_3600_4558","name":"安徽电信手机缴费(阜阳)"},{"code":"I1_3600_4559","name":"安徽电信手机缴费(黄山)"},{"code":"I1_3600_4560","name":"安徽电信手机缴费(亳州)"},{"code":"I1_3600_4561","name":"安徽电信手机缴费(淮北)"},{"code":"I1_3600_4562","name":"安徽电信手机缴费(铜陵)"},{"code":"I1_3600_4563","name":"安徽电信手机缴费(宣城)"},{"code":"I1_3600_4564","name":"安徽电信手机缴费(六安)"},{"code":"I1_3600_4566","name":"安徽电信手机缴费(池州)"}]},{"code":"I1_3600","name":"电信固话宽带缴费","type":"44","categories":[{"code":"I1_3600_2550","name":"安徽电信固话缴费(滁州)"},{"code":"I1_3600_2551","name":"安徽电信固话缴费(合肥)"},{"code":"I1_3600_2552","name":"安徽电信固话缴费(蚌埠)"},{"code":"I1_3600_2553","name":"安徽电信固话缴费(芜湖)"},{"code":"I1_3600_2554","name":"安徽电信固话缴费(淮南)"},{"code":"I1_3600_2555","name":"安徽电信固话缴费(马鞍山)"},{"code":"I1_3600_2556","name":"安徽电信固话缴费(安庆)"},{"code":"I1_3600_2557","name":"安徽电信固话缴费(宿州)"},{"code":"I1_3600_2558","name":"安徽电信固话缴费(阜阳)"},{"code":"I1_3600_2559","name":"安徽电信固话缴费(黄山)"},{"code":"I1_3600_2560","name":"安徽电信固话缴费(亳州)"},{"code":"I1_3600_2561","name":"安徽电信固话缴费(淮北)"},{"code":"I1_3600_2562","name":"安徽电信固话缴费(铜陵)"},{"code":"I1_3600_2563","name":"安徽电信固话缴费(宣城)"},{"code":"I1_3600_2564","name":"安徽电信固话缴费(六安)"},{"code":"I1_3600_2566","name":"安徽电信固话缴费(池州)"},{"code":"I1_3600_5550","name":"安徽电信宽带缴费(滁州)"},{"code":"I1_3600_5551","name":"安徽电信宽带缴费(合肥)"},{"code":"I1_3600_5552","name":"安徽电信宽带缴费(蚌埠)"},{"code":"I1_3600_5553","name":"安徽电信宽带缴费(芜湖)"},{"code":"I1_3600_5554","name":"安徽电信宽带缴费(淮南)"},{"code":"I1_3600_5555","name":"安徽电信宽带缴费(马鞍山)"},{"code":"I1_3600_5556","name":"安徽电信宽带缴费(安庆)"},{"code":"I1_3600_5557","name":"安徽电信宽带缴费(宿州)"},{"code":"I1_3600_5558","name":"安徽电信宽带缴费(阜阳)"},{"code":"I1_3600_5559","name":"安徽电信宽带缴费(黄山)"},{"code":"I1_3600_5560","name":"安徽电信宽带缴费(亳州)"},{"code":"I1_3600_5561","name":"安徽电信宽带缴费(淮北)"},{"code":"I1_3600_5562","name":"安徽电信宽带缴费(铜陵)"},{"code":"I1_3600_5563","name":"安徽电信宽带缴费(宣城)"},{"code":"I1_3600_5564","name":"安徽电信宽带缴费(六安)"},{"code":"I1_3600_5566","name":"安徽电信宽带缴费(池州)"}]},{"code":"S0_3600","name":"税费","type":"20","categories":[{"code":"S0_3600_ACP01","name":"安徽国税"}]}]

6个回答

上面输出的问题,我重新回答一下
这种比较复杂的json,许多库都不好解析,最后我是用gson来解析的。帮你写好了,你自己试试。
如果没有gson库可以到这里下载
http://download.csdn.net/detail/supolovedn/9349263

首先写一个类 TestJsonFromObject.java

 public class TestJsonFromObject {
      public String code; 
      public String name; 
      public String type; 

      List<categoriesItem> categories;

      class categoriesItem { 
        public String code; 
        public String name; 
      } 
}

在需要解析的地方写

Gson gson = new Gson(); 
List<TestJsonFromObject> ps = gson.fromJson(mJson, new TypeToken<List<TestJsonFromObject>>(){}.getType());

用for循环就可以变量list的元素了。

XS993126
这是一只熊 谢谢大神哥哥
4 年多之前 回复

求方法,本人新手菜鸟,求大神罩我

用Gson解析json字符串吧,这样简单一些。你只需要封装一个实体类就行了,或者用Android 自己提供的类jsonObject ,jsonArray 来解析你想要的字段

XS993126
这是一只熊 我做的是Java后台,不知道怎么去操作
4 年多之前 回复

我想把它转换成Javabean的话,这字符串太多了,他会不会一直覆盖

封装一个实体类ChildCode,,属性为(code,name),,在封装一个实体类为FeeCode,,属性为(code,name,type,List categories),,,就行了

Royal_lr
Royal_lr 回复XS993126: 不是少jar包吗,,加进去就行了
4 年多之前 回复
XS993126
这是一只熊 回复XS993126: 报错了
4 年多之前 回复
XS993126
这是一只熊 org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher
4 年多之前 回复

这种比较复杂的json,许多库都不好解析,最后我是用gson来解析的。帮你写好了,你自己试试。
如果没有gson库可以到这里下载
http://download.csdn.net/detail/supolovedn/9349263

首先写一个类 TestJsonFromObject.java
public class TestJsonFromObject {
public String code;
public String name;
public String type;

  List<categoriesItem> categories;

  class categoriesItem { 
    public String code; 
    public String name; 
  } 

}

在需要解析的地方写
Gson gson = new Gson();
List ps = gson.fromJson(mJson, new TypeToken>(){}.getType());
用for循环就可以变量list的元素了。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐