Java 请求获取图片链接 - 画世界 API 示例

本文演示如何使用 Java 代码解析画世界 API 返回的 JSON 数据,并提取其中 'data.multiImages[0].imgPath' 的图片链接。

1. JSON 数据

以下是一个示例 JSON 数据,包含了 'data.multiImages[0].imgPath' 的图片链接:

{
  "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=style/work_cover_med&image_process=format,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=image/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=image/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=image/resize,m_lfit,w_1080,limit_0/auto-orient,1/sharpen,100",
        "filePath": "GOmkukYB2Ap8EQ3JLZDIUW58ibRjYlIciPOFQSJa4B/5MjEPSNIntQbA73ZwH5EO3nN+Ewc6HLTPE6OF5RnG5XS2rMxirz8hHA8xFvn5wOE=",
        "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=image/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=image/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=image/resize,m_lfit,w_1080,limit_0/auto-orient,1/sharpen,100",
      "filePath": "GOmkukYB2Ap8EQ3JLZDIUW58ibRjYlIciPOFQSJa4B/5MjEPSNIntQbA73ZwH5EO3nN+Ewc6HLTPE6OF5RnG5XS2rMxirz8hHA8xFvn5wOE=",
      "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
    }
  }
}

2. Java 代码

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=style/work_cover_med&image_process=format,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=image/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=image/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=image/resize,m_lfit,w_1080,limit_0/auto-orient,1/sharpen,100",
"        "filePath": "GOmkukYB2Ap8EQ3JLZDIUW58ibRjYlIciPOFQSJa4B/5MjEPSNIntQbA73ZwH5EO3nN+Ewc6HLTPE6OF5RnG5XS2rMxirz8hHA8xFvn5wOE=",
"        "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=image/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=image/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=image/resize,m_lfit,w_1080,limit_0/auto-orient,1/sharpen,100",
"      "filePath": "GOmkukYB2Ap8EQ3JLZDIUW58ibRjYlIciPOFQSJa4B/5MjEPSNIntQbA73ZwH5EO3nN+Ewc6HLTPE6OF5RnG5XS2rMxirz8hHA8xFvn5wOE=",
"      "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
"    }
"  }
}";
        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);
    }
}

3. 代码说明

  1. 导入 org.json.JSONObject 和 org.json.JSONArray 类,用于解析 JSON 数据。
  2. 将 JSON 数据存储在一个字符串变量 json 中。
  3. 使用 new JSONObject(json) 创建一个 JSONObject 对象。
  4. 使用 getJSONObject("data") 获取 data 对象。
  5. 使用 getJSONArray("multiImages") 获取 multiImages 数组。
  6. 使用 getJSONObject(0) 获取数组中的第一个对象。
  7. 使用 getString("imgPath") 获取 imgPath 的值。
  8. 将 imgPath 打印到控制台。

4. 运行结果

图片链接: http://bsyimg.pandapaint.net/v2/work_cover/user/15281277/1694676444675.jpg?x-oss-process=image/resize,m_lfit,w_1080,limit_0/auto-orient,1/sharpen,100

总结

通过以上代码,成功从画世界 API 返回的 JSON 数据中提取了 'data.multiImages[0].imgPath' 的图片链接。

注意:

  • 请确保你的项目中已经添加了 org.json 库。
  • 实际使用时,你需要将 json 变量替换为实际获取的 JSON 数据。
Java 请求获取图片链接 - 画世界 API 示例

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

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