Json,在java后台遍历存入数据库!!!
 Object json = "{'name':'张三','xingming':'男', "
          +"'testData':{"
          +"'tqq':{'Ceshichengji':'25.6','Dengji':'优秀','Pingjiadengji':'5','Ceshixiangmumingcheng':'坐位体前屈'},"
          +"'fys':{'Ceshichengji':'0.93','Dengji':'差','Pingjiadengji':'1','Ceshixiangmumingcheng':'选择反应时'},"
          +"'fwc':{'Ceshichengji':'5','Dengji':'差','Pingjiadengji':'1','Ceshixiangmumingcheng':'俯卧撑'},"
          +"'fhl':{'Ceshichengji':'517','Dengji':'差','Pingjiadengji':'1','Ceshixiangmumingcheng':'肺活量'},"
          +"'bydjzl':{'Ceshichengji':'5','Dengji':'稍差','Pingjiadengji':'2','Ceshixiangmumingcheng':'闭眼单脚站立'},"
          +"'zt':{'Ceshichengji':'74.8','Dengji':'优秀','Pingjiadengji':'5','Ceshixiangmumingcheng':'纵跳'},"
          +"'wl':{'Ceshichengji':'8.3','Dengji':'差','Pingjiadengji':'1','Ceshixiangmumingcheng':'握力'},"
          +"'tj':{'Ceshichengji':'28.6','Dengji':'差','Pingjiadengji':'1','Ceshixiangmumingcheng':'台阶指数'},"
          +"'bl':{'Ceshichengji':'23','Dengji':'差','Pingjiadengji':'0','Ceshixiangmumingcheng':'背力'},"
          +"'tz':{'Ceshichengji':'43','Dengji':'差','Pingjiadengji':'','Ceshixiangmumingcheng':'体重'},"
          +"'sg':{'Ceshichengji':'体重','Dengji':'差','Pingjiadengji':'5','Ceshixiangmumingcheng':'身高'}}}";

这种格式怎么在后台遍历啊?

11个回答

JSONObject job = new JSONObject(s);

    System.out.println(job.get("name"));
    System.out.println(job.get("xingming"));
    JSONObject  testData = job.getJSONObject("testData");
    JSONArray arr = testData.names();
    for (int i = 0; i < arr.length(); i++) {
        JSONObject data=testData.getJSONObject((String) arr.get(i));
        System.out.print(data.get("Ceshichengji")+"\t");
        System.out.print(data.get("Dengji")+"\t");
        System.out.print(data.get("Pingjiadengji")+"\t");
        System.out.print(data.get("Ceshixiangmumingcheng")+"\t");
        System.out.println();
    }
    s就是你的字符串,不得不说你这个json串还是蛮奇葩的..解析结果:

张三

43 差 体重

5 稍差 2 闭眼单脚站立

25.6 优秀 5 坐位体前屈
8.3 差 1 握力

0.93 差 1 选择反应时
23 差 0 背力

74.8 优秀 5 纵跳

517 差 1 肺活量

5 差 1 俯卧撑

体重 差 5 身高

28.6 差 1 台阶指数
,不知道是不是你想要的

创建一个pojo
属性就是
name : String
xingming : String
testData : List>

再找个json转对象的包

先存入临时表,然后操作临时表

后面的各项指标不能改成数组形式吗?把tqq,fys,fwc等 作为个指标的一个type字段?

先转化为List再遍历啊,可以用ObjectMapper或者Gson

可以把testData这一层解析成map。

这是集合里嵌套集合么,list里包含list,创建pojo,把这个格式化啊,然后遍历数组入库,不就可以了

搞清楚数据结构,设计符合结构(需求)的表,解析json再存入库

试试将Json转化为Map集合, 然后调用Map中的数据,

用一个google的解json的包,下面是示例,写个循环就能解出来了

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = jsonParser.parse(json.toString()).getAsJsonObject();
String name = jsonObject.get("name").getAsString();
System.out.println(name);
JsonObject testData = jsonObject.get("testData").getAsJsonObject();
JsonObject bydjzl = testData.get("bydjzl").getAsJsonObject();
String bydjzl_Dengji = bydjzl.get("Dengji").getAsString();
System.out.println(bydjzl_Dengji);

============输出:============

张三
稍差

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