如何从响应主体获取数据

如何从响应主体获取数据

本文介绍了如何从响应主体获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在使用Java中的Youtube API,并设法将一些数据存储为 CommentThreadListResponse

Im working right now with Youtube API in Java, and managed to get some data stored as CommentThreadListResponse

以下是其节点的示例,但列表中包含大约100个节点。

Here is an example of its node, but list contains about 100 of them.

{
   "snippet" : {
     "topLevelComment" : {
       "snippet" : {
         "textDisplay" : "SOME COMMENT"
       }
     }
   }
 },

所以只剩下 textDisplay ,作为我想要提取到String中的东西。所以我的问题是我该怎么做?

So there is just textDisplay that remains, as something I'd like to extract into String. So my question goes "How can I do it?"

推荐答案

让我们回答并分析它,让它更容易理解我会放置一些索引值,并考虑回复评论

Let take your response and analyse it, for make it more understandable I will place some index values, and consider response comment

//index0    {
        "id": "11",
        "snippet": {
            "topLevelComment": {
                "snippet": {
                    "textDisplay": "SOME COMMENT 2 "
                }
            }
        }
    },

 //index0   {
        "id": "22",
        "snippet": {
            "topLevelComment": {
                "snippet": {
                    "textDisplay": "SOME COMMENT 2"
                }
            }
        }
    },

您将获得上述格式的回复,以获取每个代码段导航的详细信息通过索引

You will get your response in above format, so to get details of each snippet navigate through indexes

comment [0] 将提取响应的第一个元素。

comment[0] will extract the first element of the response.

comment [0] .id 将提取响应的第一个元素id 。

comment[0].id will extract the first element id of the response.

comment [0] .snippet 将提取响应的第一个片段。

comment[0].snippet will extract the first snippet of the response.

comment [0] .snippet.topLevelComment 将提取响应的第一个片段的topLevelComment。

comment[0].snippet.topLevelComment will extract the first snippet's topLevelComment of the response.

所以在这样的情况下,我们可以读取响应并获取我们需要的数据,您需要获取textDisplay,以便您可以使用以下代码,

So on like this we can read response and get the data we need in your case you need to get textDisplay so you can use following code,

comments[0].snippet.topLevelComment.snippet.textDisplay

To浏览你可以使用的所有索引,每个索引如下:

To go through all indexes you can use for-each as following

for (x in comments) {

  comments[x].snippet.topLevelComment.snippet.textDisplay

}

这篇关于如何从响应主体获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 23:31