FastJson解析Json数据

本文介绍了如何使用FastJson解析包含人体关键点信息的JSON数据。通过示例详细展示了从JSONObject到JSONArray,再到具体数据字段的获取过程,包括 JSONObject 的创建、JSONArray 的操作以及如何获取JSON内的数值。最后提供了简洁的代码实现来直接解析整个JSON数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

FastJson解析Json数据

由于我一开始搜如何解析字符串就搜的有问题,导致我一直没有把人体关键点的json数据给解析出来!!

一、首先我们先来了解一下什么是JSONArray和JSONObject

1.JSONObject和JSONArray的数据表示形式

JSONObject的数据是用 { } 来表示的。

{ "id" : "123", "courseID" : "huangt-test", "title" : "提交作业", "content" : null  }

JSONArray,顾名思义是由JSONObject构成的数组,用 [ { } , { } , … , { } ] 来表示。

[ { "id" : "123", "courseID" : "huangt-test", "title" : "提交作业" }  ,  { "content" : null, "beginTime" : 1398873600000 "endTime" } ] 

表示了包含2个JSONObject的JSONArray。

2、如何从字符串String获得JSONObject对象和JSONArray对象

JSONObject  jsonObject  = new JSONObject ( String str);
JSONArray jsonArray = new JSONArray(String str) ;

3、如何从JSONArray中获得JSONObject对象

JSONObject   jsonObject  =  jsonArray.getJSONObject(i) ;    

4、获取JSON内的数据

int   mid= jsonObject.getInt ("id") ;   
String  mcourse=jsonObject.getString("courseID") ;   

二、实战练习

{
  "person_num": 1,
  "person_info":
  [
    {
      "body_parts":
      {
        "left_hip": {"y": 1085.7919921875, "x": 539.7880859375, "score": 0.8506112098693848},
        "top_head": {"y": 36.4072265625, "x": 325.6279296875, "score": 0.9086140394210815},
        "right_mouth_corner": {"y": 229.1513671875, "x": 304.2119140625, "score": 0.9057914614677429},
        "neck": {"y": 336.2314453125, "x": 347.0439453125, "score": 0.9084023833274841},
        "left_shoulder": {"y": 421.8955078125, "x": 582.6201171875, "score": 0.885292112827301},
        "left_knee": {"y": 1578.3603515625, "x": 604.0361328125, "score": 0.8635001182556152},
        "left_ankle": {"y": 2006.6806640625, "x": 646.8681640625, "score": 0.855407178401947},
        "left_mouth_corner": {"y": 229.1513671875, "x": 368.4599609375, "score": 0.9248584508895874},
        "right_elbow": {"y": 743.1357421875, "x": 90.0517578125, "score": 0.8528175950050354},
        "right_ear": {"y": 186.3193359375, "x": 239.9638671875, "score": 0.9075623750686646},
        "nose": {"y": 186.3193359375, "x": 347.0439453125, "score": 0.9220911264419556},
        "left_eye": {"y": 143.4873046875, "x": 368.4599609375, "score": 0.9121968150138855},
        "right_eye": {"y": 143.4873046875, "x": 304.2119140625, "score": 0.9265672564506531},
        "right_hip": {"y": 1107.2080078125, "x": 282.7958984375, "score": 0.8335758447647095},
        "left_wrist": {"y": 957.2958984375, "x": 625.4521484375, "score": 0.8132022023200989},
        "left_ear": {"y": 164.9033203125, "x": 432.7080078125, "score": 0.9240163564682007},
        "left_elbow": {"y": 721.7197265625, "x": 689.7001953125, "score": 0.8721514940261841},
        "right_shoulder": {"y": 464.7275390625, "x": 132.8837890625, "score": 0.8287588357925415},
        "right_ankle": {"y": 2006.6806640625, "x": 347.0439453125, "score": 0.8630667924880981},
        "right_knee": {"y": 1599.7763671875, "x": 304.2119140625, "score": 0.8679742813110352},
        "right_wrist": {"y": 978.7119140625, "x": 175.7158203125, "score": 0.8206831812858582}
      },
        "location":
        {
          "height": 2193.0, "width": 782.5744018554688, "top": 0.0, "score": 0.9992212057113647, "left": 31.35586547851562
        }
    }
  ],
        "log_id": 1601977375525754157
}
# 1.首先最外面一层是{ },为JSONObject类型。
JSONObject jsonObject = JSON.parseObject(result);
# 2.然后person_info对应的是JSONArray类型。
JSONArray person_info = jsonObject.getJSONArray("person_info");
# 3.body_parts为JSONObject类型,取第一个元素body_parts。
# 注意这里getJSONObject的参数是0!!!
JSONObject body_parts = person_info.getJSONObject(0);
# 4.获取body_parts里具体的JSONObject
JSONObject body_parts_cemplex = body_parts.getJSONObject("body_parts");
System.out.println(body_parts_cemplex.getJSONObject("nose").getDouble("x"));

# 这里有个省略的写法
JSONObject body_parts_cemplex = JSON.parseObject(result).getJSONArray("person_info").getJSONObject(0).getJSONObject("body_parts");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值