android解析复杂json

如何解析获取video下的url_list里面的链接。

{
    "aweme_detail": {
        "admire_auth": {
            "admire_button": 0,
            "is_admire": 0,
            "is_click_admire_icon_recently": 0,
            "is_fifty_admire_author_stable_fans": 0,
            "is_show_admire_button": 0,
            "is_show_admire_tab": 0
        },
"video": {
            "big_thumbs": null,
            "bit_rate": [
                {
                    "FPS": 30,
                    "HDR_bit": "",
                    "HDR_type": "",
                    "bit_rate": 779925,
                    "gear_name": "adapt_lowest_720_1",
                    "is_bytevc1": 1,
                    "is_h265": 1,
                    "play_addr": {
                        "data_size": 652018,
                        "file_cs": "c:0-7181-bdf8|d:0-326008-b920,326009-652017-979e|a:v0d00fg10000cf42hqrc77u4gp1j3f6g",
                        "file_hash": "81b7556d3d31e961d71ee56230d53cbe",
                        "height": 1280,
                        "uri": "v0d00fg10000cf42hqrc77u4gp1j3f6g",
                        "url_key": "v0d00fg10000cf42hqrc77u4gp1j3f6g_bytevc1_720p_779925",
                        "url_list": [
                            "https://v9-cold1.douyinvod.com/4d2a019cae8d4d028475302ff0f7301d/64007e39/video/tos/cn/tos-cn-ve-15/oQNzN8uvhLBJUXfgxIvAMKBthREeB1A5IIAykx/?a=1128&ch=26&cr=3&dr=0&lr=all&cd=0%7C0%7C0%7C3&cv=1&br=761&bt=761&cs=2&ds=3&ft=GNvhKQVVywIiRZm8Zmo~xj7ScoApuJPe6vrKO230qmo0g3&mime_type=video_mp4&qs=15&rc=Nzw2NGc7ZWc6OTpkaGc5aEBpamltczc6ZnQ1aTMzNGkzM0BfLWMxLy5fNWExY2I0YjYxYSMzMC5kcjRvZTFgLS1kLWFzcw%3D%3D&l=2023030217450752D2C27E444198043BF9&btag=88000&cdn_n80=1",
                            "https://v6-cold.douyinvod.com/71b615465c8b768d9068246094fa4485/64007e39/video/tos/cn/tos-cn-ve-15/oQNzN8uvhLBJUXfgxIvAMKBthREeB1A5IIAykx/?a=1128&ch=26&cr=3&dr=0&lr=all&cd=0%7C0%7C0%7C3&cv=1&br=761&bt=761&cs=2&ds=3&ft=GNvhKQVVywIiRZm8Zmo~xj7ScoApuJPe6vrKO230qmo0g3&mime_type=video_mp4&qs=15&rc=Nzw2NGc7ZWc6OTpkaGc5aEBpamltczc6ZnQ1aTMzNGkzM0BfLWMxLy5fNWExY2I0YjYxYSMzMC5kcjRvZTFgLS1kLWFzcw%3D%3D&l=2023030217450752D2C27E444198043BF9&btag=88000",
                            "https://api.amemv.com/aweme/v1/play/?video_id=v0d00fg10000cf42hqrc77u4gp1j3f6g&line=0&file_id=4e96ed7eeaf746d78dbb182d41f309b4&sign=81b7556d3d31e961d71ee56230d53cbe&is_play_url=1&source=PackSourceEnum_AWEME_DETAIL",
                            "https://api5-normal-dsa.amemv.com/aweme/v1/play/?video_id=v0d00fg10000cf42hqrc77u4gp1j3f6g&line=1&file_id=4e96ed7eeaf746d78dbb182d41f309b4&sign=81b7556d3d31e961d71ee56230d53cbe&is_play_url=1&source=PackSourceEnum_AWEME_DETAIL"
                        ],
                        "width": 720
                    },
                    "quality_type": 15,
                    "video_extra": "{\"PktOffsetMap\":\"\"}"
                },
}

你可以使用以下代码来解析JSON并获取video下的url_list中的链接:

try {
    String json = "你的JSON数据";
    JSONObject jsonObject = new JSONObject(json);
    JSONObject videoObject = jsonObject.getJSONObject("aweme_detail").getJSONObject("video");
    JSONArray urlListArray = videoObject.getJSONObject("play_addr").getJSONArray("url_list");
    
    // 遍历url_list数组并获取链接
    for (int i = 0; i < urlListArray.length(); i++) {
        String url = urlListArray.getString(i);
        System.out.println(url);
    }
} catch (JSONException e) {
    e.printStackTrace();
}

在上面的代码中,我们首先将整个JSON数据转换为JSONObject对象。然后,我们依次获取aweme_detail和video对象。接下来,我们从play_addr对象中获取url_list数组,并遍历数组以获取每个链接。

请注意将"你的JSON数据"替换为实际的JSON字符串。另外,由于你的JSON示例中只包含了一个url_list链接,所以上面的代码只会打印出一个链接。如果有多个链接,你可以根据需要进行处理。

TechWhizKid参考GPT回答:

用像 Gson 这样的库。用 Gson 解析上述 JSON 数据的 Java

先创建一些类以匹配JSON 结构

public class PlayAddr {
    public int data_size;
    public String file_cs;
    public String file_hash;
    public int height;
    public String uri;
    public String url_key;
    public List<String> url_list;
    public int width;
}

public class BitRate {
    public int FPS;
    public String HDR_bit;
    public String HDR_type;
    public int bit_rate;
    public String gear_name;
    public int is_bytevc1;
    public int is_h265;
    public PlayAddr play_addr;
    public int quality_type;
    public String video_extra;
}

public class Video {
    public Object big_thumbs; // I'm not sure what type this should be
    public List<BitRate> bit_rate;
}

public class AwemeDetail {
    public Video video;
}

public class Root {
    public AwemeDetail aweme_detail;
}

用 Gson 解析你的 JSON 数据:

Gson gson = new Gson();
Root root = gson.fromJson(jsonString, Root.class);

最后,你可以从 root 对象中获取到你想要的 url_list:

List<String> urlList = root.aweme_detail.video.bit_rate.get(0).play_addr.url_list;

for (String url : urlList) {
    System.out.println(url);
}

jsonString 就是你的原始 JSON 数据。gson.fromJson 方法会把这个 JSON 字符串解析成 Root 类的对象。然后就通过这个对象获取到你想要的信息

使用 gson 转换成 json 对象,就非常简单了。

把你的json格式化后再发出来吧,然后用什么语言解析也说明一下

基于new bing部分指引作答:
要解析获取video下的url_list中的链接,你可以使用一种合适的编程语言,如Java或Python,并使用JSON解析库来解析复杂的JSON数据。以下是使用Python的示例代码:

import json

json_data = '''
{
    "aweme_detail": {
        "admire_auth": {
            "admire_button": 0,
            "is_admire": 0,
            "is_click_admire_icon_recently": 0,
            "is_fifty_admire_author_stable_fans": 0,
            "is_show_admire_button": 0,
            "is_show_admire_tab": 0
        },
        "video": {
            "big_thumbs": null,
            "bit_rate": [
                {
                    "FPS": 30,
                    "HDR_bit": "",
                    "HDR_type": "",
                    "bit_rate": 779925,
                    "gear_name": "adapt_lowest_720_1",
                    "is_bytevc1": 1,
                    "is_h265": 1,
                    "play_addr": {
                        "data_size": 652018,
                        "file_cs": "c:0-7181-bdf8|d:0-326008-b920,326009-652017-979e|a:v0d00fg10000cf42hqrc77u4gp1j3f6g",
                        "file_hash": "81b7556d3d31e961d71ee56230d53cbe",
                        "height": 1280,
                        "uri": "v0d00fg10000cf42hqrc77u4gp1j3f6g",
                        "url_key": "v0d00fg10000cf42hqrc77u4gp1j3f6g_bytevc1_720p_779925",
                        "url_list": [
                            "https://v9-cold1.douyinvod.com/4d2a019cae8d4d028475302ff0f7301d/64007e39/video/tos/cn/tos-cn-ve-15/oQNzN8uvhLBJUXfgxIvAMKBthREeB1A5IIAykx/?a=1128&ch=26&cr=3&dr=0&lr=all&cd=0%7C0%7C0%7C3&cv=1&br=761&bt=761&cs=2&ds=3&ft=GNvhKQVVywIiRZm8Zmo~xj7ScoApuJPe6vrKO230qmo0g3&mime_type=video_mp4&qs=15&rc=Nzw2NGc7ZWc6OTpkaGc5aEBpamltczc6ZnQ1aTMzNGkzM0BfLWMxLy5fNWExY2I0YjYxYSMzMC5kcjRvZTFgLS1kLWFzcw%3D%3D&l=2023030217450752D2C27E444198043BF9&btag=88000&cdn_n80=1",
                            "https://v6-cold.douyinvod.com/71b615465c8b768d9068246094fa4485/64007e39/video/tos/cn/tos-cn-ve-15/oQNzN8uvhLBJUXfgxIvAMKBthREeB1A5IIAykx/?a=1128&ch=26&cr=3&dr=0&lr=all&cd=0%7C0%7C0%7C3&cv=1&br=761&bt=761&cs=2&ds=3&ft=GNvhKQVVywIiRZm8Zmo~xj7ScoApuJPe6vrKO230qmo0g3&mime_type=video_mp4&qs=15&rc=Nzw2NGc7ZWc6OTpkaGc5aEBpamltczc6ZnQ1aTMzNGkzM0BfLWMxLy5fNWExY2I0YjYxYSMzMC5kcjRvZTFgLS1kLWFzcw%3D%3D&l=2023030217450752D2C27E444198043BF9&btag=88000&cdn_n80=1",
                            "https://api.amemv.com/aweme/v1/play/?video_id=v0d00fg10000cf42hqrc77u4gp1j3f6g&line=0&file_id=4e96ed7eeaf746d78dbb182d41f309b4&sign=81b7556d3d31e961d71ee56230d53cbe&is_play_url=1&source=PackSourceEnum_AWEME_DETAIL",
                            "https://api5-normal-dsa.amemv.com/aweme/v1/play/?video_id=v0d00fg10000cf42hqrc77u4gp1j3f6g&line=1&file_id=4e96ed7eeaf746d78dbb182d41f309b4&sign=81b7556d3d31e961d71ee56230d53cbe&is_play_url=1&source=PackSourceEnum_AWEME_DETAIL"
                        ],
                        "width": 720
                    },
                    "quality_type": 15,
                    "video_extra": "{\"PktOffsetMap\":\"\"}"
                }
            ]
        }
    }
}
'''

data = json.loads(json_data)
url_list = data['aweme_detail']['video']['bit_rate'][0]['play_addr']['url_list']
print(url_list)

此代码将输出包含视频链接的列表:

[
    "https://v9-cold1.douyinvod.com/4d2a019cae8d4d028475302ff0f7301d/64007e39/video/tos/cn/tos-cn-ve-15/oQNzN8uvhLBJUXfgxIvAMKBthREeB1A5IIAykx/?a=1128&ch=26&cr=3&dr=0&lr=all&cd=0%7C0%7C0%7C3&cv=1&br=761&bt=761&cs=2&ds=3&ft=GNvhKQVVywIiRZm8Zmo~xj7ScoApuJPe6vrKO230qmo0g3&mime_type=video_mp4&qs=15&rc=Nzw2NGc7ZWc6OTpkaGc5aEBpamltczc6ZnQ1aTMzNGkzM0BfLWMxLy5fNWExY2I'm sorry, but as an AI text-based model, I don't have the capability to execute code or provide a live demo. However, I can guide you on how to parse the complex JSON and extract the URLs from the "url_list" under the "video" section.

To parse the JSON and extract the URLs, you can follow these steps in any programming language that supports JSON parsing:

1. Parse the JSON: Use a JSON parsing library or function to parse the given JSON data into an object or dictionary.

2. Access the nested structure: Access the nested structure of the JSON to reach the "url_list" array. In this case, you need to access `data['aweme_detail']['video']['bit_rate'][0]['play_addr']['url_list']`.

3. Extract the URLs: Once you have access to the "url_list" array, you can iterate over its elements and extract the URLs one by one.

Here's a Python code snippet that demonstrates this:

```python
import json

json_data = '''
{ ... }  # Your JSON data here

data = json.loads(json_data)
url_list = data['aweme_detail']['video']['bit_rate'][0]['play_addr']['url_list']

# Iterate over the URLs and print them
for url in url_list:
    print(url)

请记住将 { ... } 替换为您的实际 JSON 数据。 使用 JSON 数据运行此代码,它将迭代“url_list”中的 URL 并打印它们。

如果您使用特定编程语言处理 JSON API 响应,则可能有特定于语言的库或函数可以简化 JSON 解析过程。

  1. 使用在线工具,从json获取javabean
    JSON转JAVA实体|在线JSON转JavaBean工具 - JSON.cn 提供在线,JSON转JavaBean,JSON转Java实体类,JSON转实体类,JSON转entity,JSON生成entity,json生成JavaBean文件的服务 https://www.json.cn/json/json2java.html
  2. 使用json to javabean 的jar包转换,比如jackjson,fastjson2,gson等,如下代码
         final ClassPathResource classPathResource = new ClassPathResource("test.json");
         final JsonRootBean jsonRootBean = new Gson().fromJson(new InputStreamReader(classPathResource.getInputStream(), StandardCharsets.UTF_8), JsonRootBean.class);
         jsonRootBean.getAweme_detail().getVideo().getBit_rate().stream()
                 .map(Bit_rate::getPlay_addr)
                 .map(Play_addr::getUrl_list)
                 .flatMap(List::stream)
                 .forEach(System.out::println);
    

使用JSON解析器:您可以使用一些在线的或者本地的JSON解析器,将您提供的数据作为输入,然后选择video下的url_list作为输出,就可以得到一个包含四个链接的数组。例如,您可以使用这个网站:https://jsonformatter.curiousconcept.com/ ,将您提供的数据粘贴到左边的文本框中,然后点击右边的“Parse”按钮,就可以看到解析后的结果。在结果中,您可以展开video下的bit_rate下的play_addr下的url_list,就可以看到四个链接。
使用正则表达式:您也可以使用一些支持正则表达式的文本编辑器或者编程语言,将您提供的数据作为输入,然后使用一个匹配url_list里面链接的正则表达式作为搜索条件,就可以得到四个链接。例如,您可以使用这个正则表达式:"url_list": [\s*("[^"]+")\s*,\s*("[^"]+")\s*,\s*("[^"]+")\s*,\s*("[^"]+")\s*] ,它会匹配url_list后面跟着一个包含四个双引号包围的字符串的数组。每个双引号包围的字符串就是一个链接。您可以使用这个网站:https://regex101.com/ ,将您提供的数据粘贴到左上角的文本框中,然后将上面的正则表达式粘贴到左下角的文本框中,就可以看到匹配结果。在结果中,您可以看到四个分组,每个分组对应一个链接。

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 以下回答来自chatgpt:

    你可以使用Android的内置JSON解析库来解析这个复杂的JSON数据结构。以下是一个解析JSON并获取视频链接列表的示例代码:

    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    public class JsonParser {
        public static void main(String[] args) {
            String jsonString = "{\"videos\":[{\"id\":1,\"title\":\"Video 1\",\"video\":{\"url_list\":[\"https://example.com/video1.mp4\",\"https://example.com/video1_hd.mp4\"],\"duration\":\"00:05:20\",\"size\":\"50MB\"}},{\"id\":2,\"title\":\"Video 2\",\"video\":{\"url_list\":[\"https://example.com/video2.mp4\",\"https://example.com/video2_hd.mp4\"],\"duration\":\"00:03:45\",\"size\":\"35MB\"}}]}";
    
            try {
                JSONObject jsonObject = new JSONObject(jsonString);
                JSONArray videosArray = jsonObject.getJSONArray("videos");
    
                for (int i = 0; i < videosArray.length(); i++) {
                    JSONObject videoObj = videosArray.getJSONObject(i);
                    JSONObject video = videoObj.getJSONObject("video");
                    JSONArray urlList = video.getJSONArray("url_list");
    
                    for (int j = 0; j < urlList.length(); j++) {
                        String url = urlList.getString(j);
                        System.out.println(url);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
    

    这段代码首先将JSON数据字符串转换为JSONObject对象。然后,通过getJSONArray方法从JSONObject中获取名为"videos"的JSON数组。接下来,使用循环遍历每个视频对象,并从中获取到嵌套的video对象。最后,使用getJSONArray方法获取video对象中名为"url_list"的JSON数组,再次使用循环遍历获取到每个视频链接。

    以上代码将打印出以下内容:

    https://example.com/video1.mp4
    https://example.com/video1_hd.mp4
    https://example.com/video2.mp4
    https://example.com/video2_hd.mp4
    

    这样你就可以获取到JSON数据中嵌套的所有视频链接了。希望对你有帮助!如果还有其他问题,请随时提问。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^