java后台解析json字符串

专业打酱油javaee 2017-05-19 04:05:46
json 字符数组如下
[
[ {
"trade_code": "A",
"trade_name": "%E5%86%9C%E3%80%81%E6%9E%97%E3%80%81%E7%89%A7%E3%80%81%E6%B8%94%E4%B8%9A",
"num500100": "0",
"invest500100": "0",
"num500101": "0",
"invest500101": "0",
"num500102": "0",
"invest500102": "0",
"num500103": "0",
"invest500103": "0"}]
,
[
{
"name_county": "市辖区",
"code_county": "500100"
},
{
"name_county": "万州区",
"code_county": "500101"
},
{
"name_county": "涪陵区",
"code_county": "500102"
}]

请问在后台如何拿到对应的数据
例如 "name_county": "市辖区",
"code_county": "500100"
"num500100": "0",
"invest500100": "0",
求大神给一下思路 代码
...全文
641 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_20115529 2017-05-21
  • 打赏
  • 举报
回复
学习了,挺好的。
  • 打赏
  • 举报
回复

 //y轴行业名称 加个数 加金钱
		String j = jsonArray.toString();
		//x轴区县跟编号
		String count = countyArray.toString();
        //遍历y轴
		List<Map<String, String>> list = jsonStringToList(j);		
   	    String type = "num";
   	    if(type == "invest"){
   			type = "invest";
   	    }
   	    for(int i=0;i<list.size();i++){
           
   	    }
		//遍历x轴
		List<Map<String, String>> list1 = jsonStringToList(count);
		for (int i = 0; i < list1.size(); i++) {
//			String tradeCode = list1.get(i).get("trade_code").toString();
            
			String tradeName = list1.get(i).get("trade_name").toString();
			try {
				URLDecoder.decode(tradeName, "UTF-8");//解码
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
  • 打赏
  • 举报
回复

我需要这样的格式 数据
  • 打赏
  • 举报
回复
[quote=引用 7 楼 pany1209 的回复:]

js代码:
var data=[
[ {
    "trade_code": "A",
    "trade_name": "%E5%86%9C%E3%80%81%E6%9E%97%E3%80%81%E7%89%A7%E3%80%81%E6%B8%94%E4%B8%9A",
    "num500100": "0",
    "invest500100": "0",
    "num500101": "0",
    "invest500101": "0",
    "num500102": "0",
    "invest500102": "0",
    "num500103": "0",
    "invest500103": "0"}]
,
[
{
    "name_county": "市辖区",
    "code_county": "500100"
},
{
    "name_county": "万州区",
    "code_county": "500101"
},
{
    "name_county": "涪陵区",
    "code_county": "500102"
}]];
	var send="params="+JSON.stringify(data);
	  $.post(url,send,function(back){
	    	alert(back);
	    }); 
controller代码:
 public void requestJson(String params){
        System.out.println(params);
       // params=params.substring(params.indexOf("[")+1, params.lastIndexOf("]"));
        System.out.println(params);
        ObjectMapper mapper=new ObjectMapper();
        try {
			List<List<HashMap<String, Object>>> list=mapper.readValue(params, mapper.getTypeFactory().constructCollectionType(List.class, new ArrayList<HashMap<String,Object>>().getClass()));
			String code=(String) list.get(1).get(2).get("code_county");
			String name=(String) list.get(1).get(2).get("name_county");
			System.out.println(code);
			System.out.println(name);
			System.out.println(list.get(0).get(0).get("num"+code));
			System.out.println(list.get(0).get(0).get("invest"+code));
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
你这个写的确实厉害 不过我看不懂- - 能不能用最简单list<map>的遍历出来
李德胜1995 2017-05-19
  • 打赏
  • 举报
回复

js代码:
var data=[
[ {
    "trade_code": "A",
    "trade_name": "%E5%86%9C%E3%80%81%E6%9E%97%E3%80%81%E7%89%A7%E3%80%81%E6%B8%94%E4%B8%9A",
    "num500100": "0",
    "invest500100": "0",
    "num500101": "0",
    "invest500101": "0",
    "num500102": "0",
    "invest500102": "0",
    "num500103": "0",
    "invest500103": "0"}]
,
[
{
    "name_county": "市辖区",
    "code_county": "500100"
},
{
    "name_county": "万州区",
    "code_county": "500101"
},
{
    "name_county": "涪陵区",
    "code_county": "500102"
}]];
	var send="params="+JSON.stringify(data);
	  $.post(url,send,function(back){
	    	alert(back);
	    }); 
controller代码:
 public void requestJson(String params){
        System.out.println(params);
       // params=params.substring(params.indexOf("[")+1, params.lastIndexOf("]"));
        System.out.println(params);
        ObjectMapper mapper=new ObjectMapper();
        try {
			List<List<HashMap<String, Object>>> list=mapper.readValue(params, mapper.getTypeFactory().constructCollectionType(List.class, new ArrayList<HashMap<String,Object>>().getClass()));
			String code=(String) list.get(1).get(2).get("code_county");
			String name=(String) list.get(1).get(2).get("name_county");
			System.out.println(code);
			System.out.println(name);
			System.out.println(list.get(0).get(0).get("num"+code));
			System.out.println(list.get(0).get(0).get("invest"+code));
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
这种写死了。。。参考一下
鱿鱼ing 2017-05-19
  • 打赏
  • 举报
回复
500100" "num500100": "0", "invest500100": "0", 除了500100数字一样 没有其他必然联系啊 试试json或者map的 key 模糊匹配
  • 打赏
  • 举报
回复
不需要实体类 放到map里就好
  • 打赏
  • 举报
回复
我不知道怎么遍历对应上
  • 打赏
  • 举报
回复
我主要是不知道如何遍历出来对应上
很叼么 2017-05-19
  • 打赏
  • 举报
回复
<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.3</version>
		</dependency>
public class Test {
    @org.junit.Test
    public void json () {
    /*    GroupTextMessage msg = new GroupTextMessage(100, "很叼么");
        String jsonOut = JSON.toJSONString(msg);
        System.out.println(jsonOut);*/

        J j = JSON.parseObject("{age:11}", J.class);
        System.out.println(j);

    }
}
class J {
    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
引入阿里的json,再创建对应的json的bena,再toObject就行
很叼么 2017-05-19
  • 打赏
  • 举报
回复
引入json包,把json转为object啊,建一个对应的bean

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧