为了账号安全,请及时绑定邮箱和手机立即绑定

打印键值不能按预期使用 JSONObject

打印键值不能按预期使用 JSONObject

牧羊人nacy 2021-10-27 16:50:42
我试图打印出下面 JSON 中的所有键值,但无论出于何种原因,它只为我打印第一组值数组。我怎样才能打印出 idValue 字符串中的所有键值?编辑:我现在收到错误:意外错误:JSONObject["submenu"] 不是字符串。代码import com.google.common.collect.LinkedListMultimap;import com.google.common.collect.Multimap;import org.json.JSONArray;import org.json.JSONObject;import java.util.Map;public class testing {    static Multimap<String, String> allMappedKeyValues = LinkedListMultimap.create();    public static void main(String[] args) {        String idValue = "[{\"link\": \"/us_new/en/home\",\"amid\": \"1__home\",\"title\": \"Home\"}, {\"link\": \"/us_new/en/home/diagnosis\",\"amid\": \"2__diagnosis\",\"title\": \"Diagnosis\"}, {\"link\": \"/us_new/en/home/loss\",\"amid\": \"3__loss\",\"title\": \"loss\",\"submenu\": [{\"amid\": \"4__quiz\",\"name\": \"quiz\",\"title\": \"quiz\"},{\"amid\": \"5__questions\",\"name\": \"questions\",\"title\": \"Questions\"}]}]";        JSONArray array = new JSONArray(idValue);        for (int i = 0; i < array.length(); i++)        {            JSONObject object = array.getJSONObject(i);            JSONArray keys = object.names();            for (int j = 0; j < keys.length(); ++j)            {                String key = keys.getString(j);                Object value = object.get(key);                if (value instanceof JSONArray) {                    JSONArray array1 = (JSONArray) value;                    for (int k = 0; k < array1.length(); k++) {                        JSONObject object1 = array1.getJSONObject(k);                        JSONArray keys1 = object1.names();
查看完整描述

3 回答

?
慕标5832272

TA贡献1966条经验 获得超4个赞

您不需要替换[],您的 JSON 格式正确。您实际上可以JSONArray直接从字符串创建,


String idValue = "[{\"link\":\"/us_new/en/home\",\"amid\":\"1__home\",\"title\":\"Home\"},{\"link\":\"/us_new/en/home/diagnosis\",\"amid\":\"2__diagnosis\",\"title\":\"Diagnosis\"},{\"link\":\"/us_new/en/home/treatment\",\"amid\":\"3__loss\",\"title\":\"loss\"}]";

JSONArray objects = new JSONArray(idValue);

for(int i = 0; i < objects.length(); i++) {

    JSONObject jsonObject = objects.getJSONObject(i);


    Iterator<String> keys = jsonObject.keys();

    while(keys.hasNext()) {

        String key = keys.next();

        System.out.printf("key : %s | value : %s\n", key, jsonObject.get(key));

    }

}

输出 :


key : link | value : /us_new/en/home

key : amid | value : 1__home

key : title | value : Home

key : link | value : /us_new/en/home/diagnosis

key : amid | value : 2__diagnosis

key : title | value : Diagnosis

key : link | value : /us_new/en/home/treatment

key : amid | value : 3__loss

key : title | value : loss


查看完整回答
反对 回复 2021-10-27
?
婷婷同学_

TA贡献1844条经验 获得超8个赞

您可以使用以下递归函数“parseJsonArray”来解析嵌套的 jsonArrays。


String idValue = "[{\"link\": \"/us_new/en/home\",\"amid\": \"1__home\",\"title\": \"Home\"}, {\"link\": \"/us_new/en/home/diagnosis\",\"amid\": \"2__diagnosis\",\"title\": \"Diagnosis\"}, {\"link\": \"/us_new/en/home/loss\",\"amid\": \"3__loss\",\"title\": \"loss\",\"submenu\": [{\"amid\": \"4__quiz\",\"name\": \"quiz\",\"title\": \"quiz\"},{\"amid\": \"5__questions\",\"name\": \"questions\",\"title\": \"Questions\"}]}]";


parseJsonArray(idValue);


private void parseJsonArray(String jsonString) throws JSONException {

    System.out.println("jsonString: " + jsonString);


    JSONArray objects = new JSONArray(jsonString);

    JSONArray  nestedJsonArray;

    JSONObject  nestedJsonObject;


    for(int i = 0; i < objects.length(); i++) {

        JSONObject jsonObject = objects.getJSONObject(i);

        Iterator<String> keys = jsonObject.keys();


        while(keys.hasNext()) {

            String key = keys.next();                

            Object nestedObject = jsonObject.get(key);

            if (nestedObject instanceof JSONArray) {

                System.out.println("it's an array: " + key);

                nestedJsonArray = (JSONArray)nestedObject;

                parseJsonArray(nestedJsonArray.toString());

            }

            else if (nestedObject instanceof JSONObject) {

                nestedJsonObject = (JSONObject)nestedObject;

                System.out.printf("key : %s | value : %s | title : %s\n", key, nestedJsonObject.getString(key), nestedJsonObject.getString("title"));

            }

            else {

                System.out.printf("key : %s | value : %s | title : %s\n", key, jsonObject.getString(key), jsonObject.getString("title"));

            }

        }

    }


查看完整回答
反对 回复 2021-10-27
?
陪伴而非守候

TA贡献1757条经验 获得超8个赞

您只读取了第一个 JSON 对象 ( {\"link\":\"/us_new/en/home\",\"amid\":\"1__home\",\"title\":\"Home\"})。为了读取所有 3 个对象,首先将 JSON 字符串更改为数组(添加缺少的[和]):


String idValue = "[{\"link\":\"/us_new/en/home\",\"amid\":\"1__home\",\"title\":\"Home\"},{\"link\":\"/us_new/en/home/diagnosis\",\"amid\":\"2__diagnosis\",\"title\":\"Diagnosis\"},{\"link\":\"/us_new/en/home/treatment\",\"amid\":\"3__loss\",\"title\":\"loss\"}]";

然后遍历数组:


JSONArray array = new JSONArray(idValue);

for (int i = 0; i < array.length (); i++) {

    JSONObject object = array.getJSONObject (i);

    JSONArray keys = object.names();

    for (int j = 0; j < keys.length(); ++j) {

        System.out.println("Entered loop :"+j);

        String key = keys.getString(j);

        String value = object.getString(key);

        String title = object.getString("title");

        System.out.println("-key :" + key + "\n-value " + value + "\n-title :" + title);

    }

}

输出:


Entered loop :0

-key :link

-value /us_new/en/home

-title :Home

Entered loop :1

-key :amid

-value 1__home

-title :Home

Entered loop :2

-key :title

-value Home

-title :Home

Entered loop :0

-key :link

-value /us_new/en/home/diagnosis

-title :Diagnosis

Entered loop :1

-key :amid

-value 2__diagnosis

-title :Diagnosis

Entered loop :2

-key :title

-value Diagnosis

-title :Diagnosis

Entered loop :0

-key :link

-value /us_new/en/home/treatment

-title :loss

Entered loop :1

-key :amid

-value 3__loss

-title :loss

Entered loop :2

-key :title

-value loss

-title :loss


查看完整回答
反对 回复 2021-10-27
  • 3 回答
  • 0 关注
  • 237 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信