猿问

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

我试图打印出下面 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();



牧羊人nacy
浏览 238回答 3
3回答

慕标5832272

您不需要替换[],您的 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++) {&nbsp; &nbsp; JSONObject jsonObject = objects.getJSONObject(i);&nbsp; &nbsp; Iterator<String> keys = jsonObject.keys();&nbsp; &nbsp; while(keys.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; String key = keys.next();&nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("key : %s | value : %s\n", key, jsonObject.get(key));&nbsp; &nbsp; }}输出 :key : link | value : /us_new/en/homekey : amid | value : 1__homekey : title | value : Homekey : link | value : /us_new/en/home/diagnosiskey : amid | value : 2__diagnosiskey : title | value : Diagnosiskey : link | value : /us_new/en/home/treatmentkey : amid | value : 3__losskey : title | value : loss

婷婷同学_

您可以使用以下递归函数“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 {&nbsp; &nbsp; System.out.println("jsonString: " + jsonString);&nbsp; &nbsp; JSONArray objects = new JSONArray(jsonString);&nbsp; &nbsp; JSONArray&nbsp; nestedJsonArray;&nbsp; &nbsp; JSONObject&nbsp; nestedJsonObject;&nbsp; &nbsp; for(int i = 0; i < objects.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonObject = objects.getJSONObject(i);&nbsp; &nbsp; &nbsp; &nbsp; Iterator<String> keys = jsonObject.keys();&nbsp; &nbsp; &nbsp; &nbsp; while(keys.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String key = keys.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object nestedObject = jsonObject.get(key);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (nestedObject instanceof JSONArray) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("it's an array: " + key);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nestedJsonArray = (JSONArray)nestedObject;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parseJsonArray(nestedJsonArray.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (nestedObject instanceof JSONObject) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nestedJsonObject = (JSONObject)nestedObject;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("key : %s | value : %s | title : %s\n", key, nestedJsonObject.getString(key), nestedJsonObject.getString("title"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("key : %s | value : %s | title : %s\n", key, jsonObject.getString(key), jsonObject.getString("title"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

陪伴而非守候

您只读取了第一个 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++) {&nbsp; &nbsp; JSONObject object = array.getJSONObject (i);&nbsp; &nbsp; JSONArray keys = object.names();&nbsp; &nbsp; for (int j = 0; j < keys.length(); ++j) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Entered loop :"+j);&nbsp; &nbsp; &nbsp; &nbsp; String key = keys.getString(j);&nbsp; &nbsp; &nbsp; &nbsp; String value = object.getString(key);&nbsp; &nbsp; &nbsp; &nbsp; String title = object.getString("title");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("-key :" + key + "\n-value " + value + "\n-title :" + title);&nbsp; &nbsp; }}输出:Entered loop :0-key :link-value /us_new/en/home-title :HomeEntered loop :1-key :amid-value 1__home-title :HomeEntered loop :2-key :title-value Home-title :HomeEntered loop :0-key :link-value /us_new/en/home/diagnosis-title :DiagnosisEntered loop :1-key :amid-value 2__diagnosis-title :DiagnosisEntered loop :2-key :title-value Diagnosis-title :DiagnosisEntered loop :0-key :link-value /us_new/en/home/treatment-title :lossEntered loop :1-key :amid-value 3__loss-title :lossEntered loop :2-key :title-value loss-title :loss
随时随地看视频慕课网APP

相关分类

Java
我要回答