首页 > 其他分享 >json类型数据取出想要的部分

json类型数据取出想要的部分

时间:2022-10-27 20:48:38浏览次数:45  
标签:name JSONObject id json score result 取出 想要 log

因为才疏学浅,只能用很笨的方法。
以下是我拿到的数据的json型数据。
{"result":{"ingredient":{"result":[{"score":0.9999989,"name":"香蕉"},{"score":7.754284E-7,"name":"小米蕉"},{"score":1.252173E-7,"name":"芭蕉"},{"score":5.0951E-8,"name":"蒲瓜"},{"score":2.0919343E-8,"name":"茄子"}],"log_id":1585606298733704458,"result_num":5},"red_wine":{"result":{"wineNameCn":"","hasdetail":0},"log_id":1585606299218276580},"plant":{"result":[{"score":0.8585518,"name":"香蕉"},{"score":0.013570859,"name":"芭蕉"},{"score":0.0024424153,"name":"大蕉"}],"log_id":1585606300248077894},"animal":{"result":[{"score":"0.999349","name":"非动物"}],"log_id":1585606298772283861},"currency":{"result":{"currencyName":"","hasdetail":0},"log_id":1585606300350185185},"dishs":{"result":[{"probability":"0.992685","has_calorie":false,"name":"非菜"}],"log_id":1585606299300899292,"result_num":1},"landmark":{"result":{"landmark":""},"log_id":1585606300247546709}},"log_id":16668729751268258}
我想拿到第一位的香蕉。

点击查看代码
JSONObject object= JSONObject.parseObject(result);//转化成JSONObject型,取出plant字段。
            JSONObject a =object.getJSONObject("result").getJSONObject("plant");
            System.out.println(a);//看看取出来的是不是想要的。
            JSONObject b=a.getJSONArray("result").getJSONObject(0);//因为它里面是一个数组,所以想要单独拿出来就得做一个array类型转换,并且取到第一个值。
            System.out.println(b.getString("name"));//查看你想要的字段。

注释即是思路!

标签:name,JSONObject,id,json,score,result,取出,想要,log
From: https://www.cnblogs.com/jiankaitianmen/p/16833642.html

相关文章