gpt4 book ai didi

java - 通过名称获取 JSON 对象值 Java

转载 作者:行者123 更新时间:2023-12-01 11:20:12 26 4
gpt4 key购买 nike

我的 JSON 数据看起来像这样:

[
"{\"pid\":\"1\",\"title\":\"New CEO announced\",\"titleirish\":\"CEO nua\",\"content\":\"Bernard Byrne has been announced as the new CEO. Rejoice!\",\"contentirish\":\"Is Bernard Byrne an CEO. B\\\\u00edg\\\\u00ed s\\\\u00e1sta!\",\"imageurl\":\"http:\\\\\\/\\\\\\/scoiluiriada.ie\\\\\\/wp-content\\\\\\/uploads\\\\\\/2014\\\\\\/02\\\\\\/IMG_1781-150x112.jpg\",\"category\":\"News\",\"publishedby\":\"Andy\",\"modified\":\"2015-07-01 16:21:13\",\"buildings\":\"Bankcentre,Hume House,Time House\"}",
"{\"pid\":\"2\",\"title\":\"New CTO pronounced\",\"titleirish\":\"CEO nua\",\"content\":\"Bernard Byrne has been announced as the new CEO. Rejoice!\",\"contentirish\":\"Is Bernard Byrne an CEO. B\\\\u00edg\\\\u00ed s\\\\u00e1sta!\",\"imageurl\":\"http:\\\\\\/\\\\\\/scoiluiriada.ie\\\\\\/wp-content\\\\\\/uploads\\\\\\/2014\\\\\\/02\\\\\\/IMG_1781-150x112.jpg\",\"category\":\"News\",\"publishedby\":\"Andy\",\"modified\":\"2015-07-02 10:09:10\",\"buildings\":\"Hume House\"}",
....

到目前为止,我有以下代码:

    JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(allData); // allData = JSON String above
JSONArray nitems = (JSONArray)obj;

这给了我一个包含我想要解析的所有对象的数组。 (效果很好)

现在,循环遍历这个数组,我想获取对象成员值。像这样的事情:

nitems.get(0).getValueOf("title") // should return "New CEO announced"
nitems.get(0).getValueOf("titleirish") // "CEO Nua" etc.

这显然不起作用,我应该使用什么代码来代替。

感谢您的提前答复。

最佳答案

在一些评论的帮助下修复了。这是解决方案:

JSONObject obj2 = (JSONObject)new JSONParser().parse(nitems.get(i).toString());
System.out.println(obj2.get("title").toString());

关于java - 通过名称获取 JSON 对象值 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31336604/

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