以下代码展示了如何使用 Java 的 JSONObject 类来解析 JSON 并获取所需的数据,代码示例使用了 org.json 库来解析 JSON。

import org.json.JSONArray;
import org.json.JSONObject;

public class Main {
    public static void main(String[] args) {
        String json = "{
" + "  "status": 1,
" + "  "msg": "success",
" + "  "data": {
" + "    "id": 180830853,
" + "    "userId": 15281277,
" + "    "status": 1,
" + "    "targetType": 2,
" + "    "targetValue": 72924806,
" + "    "content": '可以求个花花嘛(ㅇㅅㅇ❀)',
" + "    "commentCount": 19,
" + "    "tags": "二创,稿,插画",
" + "    "isLike": 0,
" + "    "isCollect": 0,
" + "    "createTime": "2023-09-14 15:27:29",
" + "    "hasPic": 1,
" + "    "recommendType": 4,
" + "    "factionIds": "20,3830,1583",
" + "    "app": 1,
" + "    "isToped": false,
" + "    "topTime": "2023-09-14 15:27:29",
" + "    "likeCount": 113,
" + "    "hotPoint": 188,
" + "    "giftCount": 15,
" + "    "user": {
" + "      "id": 15281277,
" + "      "srcType": 2,
" + "      "createTime": "2022-01-29 23:43:26",
" + "      "nick": "小小小漱",
" + "      "avatar": "http://bsyimgv2.pandapaint.net/v2/avatar/user/15281277/1694675609980.png?x-oss-process\u003dstyle/work_cover_med\u0026image_process\u003dformat,WEBP",
" + "      "sex": 1,
" + "      "level": 4,
" + "      "addr": "水果星",
" + "      "instruction": "",
" + "      "certificationSig": 0,
" + "      "certificationDes": "",
" + "      "isFrost": 0,
" + "      "worksCount": 13,
" + "      "fansCount": 243,
" + "      "attentionCount": 37,
" + "      "vipId": 1,
" + "      "vipStartTime": "2023-09-14 15:19:32",
" + "      "vipExpireTime": "2023-10-14 15:19:32",
" + "      "vipLevel": 1,
" + "      "backgroundUrl": "http://bsyimg.pandapaint.net/background/2023/03/19/AE1F3A7D55B84A498A43C9C810F0CFCD.jpg",
" + "      "lastLoginTime": "2023-09-16 22:24:19",
" + "      "exp": 1655,
" + "      "app": 1,
" + "      "headBox": 3,
" + "      "isFollow": 0,
" + "      "maskState": 0,
" + "      "realName": "小小小漱",
" + "      "profile": []
" + "    },
" + "    "targetStatus": 1,
" + "    "minAge": 0,
" + "    "multiImages": [
" + "      {
" + "        "type": 1,
" + "        "imgPath": "http://bsyimg.pandapaint.net/v2/work_cover/user/15281277/1694676444675.jpg?x-oss-process\u003dimage/resize,m_lfit,w_1080,limit_0/auto-orient,1/sharpen,100",
" + "        "noWmPath": "http://bsyimg.pandapaint.net/v2/work_cover/user/15281277/1694676444675.jpg?x-oss-process\u003dimage/resize,m_lfit,w_1080,limit_0/auto-orient,1/sharpen,100",
" + "        "orgPath": "http://bsyimg.pandapaint.net/v2/work_cover/user/15281277/1694676444675.jpg",
" + "        "orgWmPath": "http://bsyimg.pandapaint.net/v2/work_cover/user/15281277/1694676444675.jpg",
" + "        "wmPath": "http://bsyimg.pandapaint.net/v2/work_cover/user/15281277/1694676444675.jpg?x-oss-process\u003dimage/resize,m_lfit,w_1080,limit_0/auto-orient,1/sharpen,100",
" + "        "filePath": "GOmkukYB2Ap8EQ3JLZDIUW58ibRjYlIciPOFQSJa4B/5MjEPSNIntQbA73ZwH5EO3nN+Ewc6HLTPE6OF5RnG5XS2rMxirz8hHA8xFvn5wOE\u003d",
" + "        "width": 1618,
" + "        "height": 2157,
" + "        "device": "画世界_iOS 1618x2157",
" + "        "totalLineCount": 9426,
" + "        "paintVer": 31,
" + "        "coverWidth": 1618,
" + "        "coverHeight": 2157,
" + "        "paintPkgSize": 10639992,
" + "        "videoPath": "",
" + "        "imgId": 72924806
" + "      }
" + "    ],
" + "    "pic": {
" + "      "type": 1,
" + "      "imgPath": "http://bsyimg.pandapaint.net/v2/work_cover/user/15281277/1694676444675.jpg?x-oss-process\u003dimage/resize,m_lfit,w_1080,limit_0/auto-orient,1/sharpen,100",
" + "      "noWmPath": "http://bsyimg.pandapaint.net/v2/work_cover/user/15281277/1694676444675.jpg?x-oss-process\u003dimage/resize,m_lfit,w_1080,limit_0/auto-orient,1/sharpen,100",
" + "      "orgPath": "http://bsyimg.pandapaint.net/v2/work_cover/user/15281277/1694676444675.jpg",
" + "      "orgWmPath": "http://bsyimg.pandapaint.net/v2/work_cover/user/15281277/1694676444675.jpg",
" + "      "wmPath": "http://bsyimg.pandapaint.net/v2/work_cover/user/15281277/1694676444675.jpg?x-oss-process\u003dimage/resize,m_lfit,w_1080,limit_0/auto-orient,1/sharpen,100",
" + "      "filePath": "GOmkukYB2Ap8EQ3JLZDIUW58ibRjYlIciPOFQSJa4B/5MjEPSNIntQbA73ZwH5EO3nN+Ewc6HLTPE6OF5RnG5XS2rMxirz8hHA8xFvn5wOE\u003d",
" + "      "width": 1618,
" + "      "height": 2157,
" + "      "device": "画世界_iOS 1618x2157",
" + "      "totalLineCount": 9426,
" + "      "paintVer": 31,
" + "      "coverWidth": 1618,
" + "      "coverHeight": 2157,
" + "      "paintPkgSize": 10639992,
" + "      "videoPath": "",
" + "      "imgId": 72924806,
" + "      "animFrameCount": 0
" + "    },
" + "    "isMultiPaint": false,
" + "    "faction": [
" + "      {
" + "        "id": 20,
" + "        "name": "二创",
" + "        "type": 2,
" + "        "isToped": false,
" + "        "isBoutique": false,
" + "        "coverUrl": "http://bsyimg.pandapaint.net/images/topic/%E4%BA%8C%E5%88%9B.png",
" + "        "canBlock": false
" + "      },
" + "      {
" + "        "id": 3830,
" + "        "name": "稿",
" + "        "type": 2,
" + "        "isToped": false,
" + "        "isBoutique": false,
" + "        "coverUrl": "http://bsyimg.pandapaint.net/topic/2018/09/25/a100af90891e4c5e9c04fe968e36eaa1.jpg",
" + "        "canBlock": true
" + "      },
" + "      {
" + "        "id": 1583,
" + "        "name": "插画",
" + "        "type": 2,
" + "        "isToped": false,
" + "        "isBoutique": false,
" + "        "coverUrl": "http://bsyimg.pandapaint.net/topic/2018/09/15/4cf8256ae3094fc280d976024913e257.jpg",
" + "        "canBlock": true
" + "      }
" + "    ],
" + "    "noScreenshot": "",
" + "    "recommend": {
" + "      "recommendType": 4,
" + "      "recommendStyle": 1,
" + "      "recommendTypeName": "人物",
" + "      "isRecommended": false,
" + "      "isScored": false,
" + "      "recommendState": 5
" + "    }
" + "  }
" + "}";
        try {
            JSONObject jsonObject = new JSONObject(json);
            JSONObject data = jsonObject.getJSONObject("data");
            JSONArray multiImages = data.getJSONArray("multiImages");
            JSONObject firstImage = multiImages.getJSONObject(0);
            String imgPath = firstImage.getString("imgPath");
            System.out.println("imgPath: " + imgPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这段代码首先将 JSON 字符串解析为 JSONObject 对象,然后依次获取 datamultiImagesmultiImages[0]imgPath 的值,最终输出 imgPath 的值。

这只是一个示例,你可以根据实际需求修改代码来获取其他参数。

注意: 以上代码使用了 org.json 库,需要先添加该库到你的项目中。

Java 请求获取画世界作品详情 API 接口

原文地址: https://www.cveoy.top/t/topic/iIPy 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录