gpt4 book ai didi

java - 我如何使用 Java 解析 Wikiquotes 响应?

转载 作者:行者123 更新时间:2023-11-30 00:38:40 25 4
gpt4 key购买 nike

我尝试通过 Java 访问 Wikiquote 来获取引文。

到目前为止我有这段代码:

 JsonObjectRequest request = new JsonObjectRequest(
Request.Method.GET, "https://en.wikiquote.org/w/api.php?format=json&action=parse&page=Ellen_DeGeneres&prop=text", null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jobject) {
try {
for (int i = 0; i < jobject.names().length(); i++) {
Log.e("JSON", "key = " + jobject.names().getString(i) + " value = " + jobject.get(jobject.names().getString(i)));
}

} catch (Exception ex) {
Log.e("JSON", ex.getLocalizedMessage());
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
});
request.setTag(getClass().getName());
VolleySingleton.getInstance(this).addToRequestQueue(request);

这是我的 logcat 中的响应。所以它应该是 JSON 响应,但正文对我来说看起来很像 HTML。那么我如何解析它以获得所有引号呢?

key = parse value = {"title":"Ellen DeGeneres","pageid":1902,"text":{"*":"\n</a>\n\n</a></div>\nEllen DeGeneres</div>\n</div>\n</div>\n

Ellen Lee DeGeneres</a></b> (born January 26</a>, 1958</a>) is an American stand-up comedienne, television hostess and actress. She starred in the popular sitcom Ellen</a></i> from 1994 to 1998, and has hosted her syndicated TV talk show, The Ellen DeGeneres Show</a></i>, since 2003. She is married to Portia de Rossi</a>.</p>\nQuotes</span>[</span>edit</a>]</span></span></h2>\n\n

  • I think the hard thing about this job [stand-up] — I mean, I think this part is great — but that the traveling is y'know, 'cause — 'cause I'm gone a lot from home and this time I'm out for three-and-a-half weeks without going home, and that's hard, to be gone three-and-a-half weeks 'cause then I have to ask my friends, \"Would you mind going to the house and watering the plants, and turn some lights on and make it look like somebody's home, and make sure that the mobile over the crib isn't tangled or the baby's gonna get bored...\"\n\n
  • Taste This</i></li>\n</ul>\n</li>\n</ul>\n\n
  • Don't you hate when people are late to work. And they always have the worst excuses. \"Oh, I'm sorry I'm late, traffic.\" \"Traffic, huh? How do you think I got here; helicoptered in!?\"\n\n
  • Here and Now</i></li>\n</ul>\n</li>\n</ul>\n\n
  • I'm a — I'm a, um, a godmother which is just, that's fun to be a godmother, she is so</i> precious, she's the light of my life, she's two... or five or something, and she's, uh... I don't know, I've never seen her — the pictures are precious, she just seems so, y'know... She lives clear across town, I don't have that kind of time, but, um... Well, I send money and stuff, it's not like I don't have a connection....\n\n
  • Taste This</i></li>\n</ul>\n</li>\n</ul>\n\n
  • I don't want to get the same looks I give people when they get on a plane holding a baby: \"That's a cute baby, just keep walking, keep walking, keep going, keep going....\"\n\n
  • Taste This</i></li>\n</ul>\n</li>\n</ul>\n\n
  • If we don't want to define ourselves by things as superficial as our appearances, we're stuck with the revolting alternative of being judged by our actions, by what we do.\n\n
  • My Point... And I Do Have One</i>. New York: Bantam Books, 1995</li>\n</ul>\n</li>\n</ul>\n\n
  • 最佳答案

    维基百科首先是面向 HTML 的,如果我们可以这么说的话,并且将 HTML 转换为 JSON 没有什么意义(修辞:顺便说一下,哪种 JSON 格式?),所以它只是返回 HTML 的一部分显示在任意 HTML 查看器中。你在这里要做的是:

    • 进行 API 调用以获得格式正确的 JSON 响应(您正在使用 Volley 进行此操作)。
    • 从 JSON 响应中提取引号 HTML。
    • 从上一步提取的 HTML 中提取引号。
    • 以某种方式显示引号。

    我刚刚转储了截至 2017 年 4 月 5 日的响应,以下示例提出了一种可能的解决方案,即使用带有 org.json 和 JSoup 的普通 Java。 :

    public static void main(final String... args)
    throws IOException {
    try ( final Reader reader = getPackageResourceReader(Q42938530.class, "wikiquotes.json") ) {
    final JSONTokener tokener = new JSONTokener(reader);
    final JSONObject jsonObject = (JSONObject) tokener.nextValue();
    final String quotesHtml = extractQuotesHtml(jsonObject);
    final List<String> quotes = extractQuotes(quotesHtml);
    for ( final String quote : quotes ) {
    System.out.println(quote);
    }
    }
    }

    private static String extractQuotesHtml(final JSONObject jsonObject) {
    return jsonObject
    .getJSONObject("parse")
    .getJSONObject("text")
    .getString("*");
    }

    private static List<String> extractQuotes(final String quotesHtml) {
    final Document document = Jsoup.parse(quotesHtml);
    final List<String> quotes = new ArrayList<>();
    final Elements elements = document.select("ul > li");
    for ( final Element element : elements ) {
    quotes.add(element.text());
    }
    return quotes;
    }

    当然,您必须微调extractQuotes 方法以更准确地提取引号。目前,上面的示例能够向标准输出生成以下输出:

    I think the hard thing about this job [stand-up] — I mean, I think this part is great — but that the traveling is y'know, 'cause — 'cause I'm gone a lot from home and this time I'm out for three-and-a-half weeks without going home, and that's hard, to be gone three-and-a-half weeks 'cause then I have to ask my friends, "Would you mind going to the house and watering the plants, and turn some lights on and make it look like somebody's home, and make sure that the mobile over the crib isn't tangled or the baby's gonna get bored..." Taste This
    Taste This
    Don't you hate when people are late to work. And they always have the worst excuses. "Oh, I'm sorry I'm late, traffic." "Traffic, huh? How do you think I got here; helicoptered in!?" Here and Now
    Here and Now

    关于java - 我如何使用 Java 解析 Wikiquotes 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42938530/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com