使用java访问json对象中的列表

我有以下 json 字符串,并想使用 Java 访问 alsoKnownAs 列表。我可以使用 getString 作为名称,我已经为 alsoKnownAs 尝试过 getJSONArray 但它并没有完全锻炼


{\"name\":\"moses\",\"alsoKnownAs\":[\"njai\", \"njenga\",\"musa\"]}

我可以访问下面的名称,但我不能使用等效的 getString 方法返回字符串列表或等效的 getJSONArray 以获取字符串列表


    public static Person parsePersonJson(String json) {


            JSONObject currentPerson;

            String name;


            try {

                currentPerson = new JSONObject(json);


                // so I can access the name like


                name = currentPerson.getString("name");


               //I was trying this to get the list but figure out I was using a list of json objects, so not how to get the list of stings


           JSONArray arrayKnownAs = names.getJSONArray("alsoKnownAs");

                List<String> alsoKnownAs= new ArrayList<>();


                for (int i = 0, l = arrayKnownAs.length(); i < l; i++) {

                    String origin;

                    origin = arrayKnownAs[i];

                    alsoKnownAs.add(origin);

                }




               Person thisPerson =  new Person(


                //I instantiate person object here


                );

                return thisPerson;


            } catch (org.json.JSONException e) {

    // error

            }

            return null;

        }


料青山看我应如是
浏览 172回答 2
2回答

弑天下

如果其他人被困在这里,结果证明我在正确的轨道上,我可以使用 getJSONArray 访问列表,但是在为每个成员迭代时,我使用 getString&nbsp;public static Person parsePersonJson(String json) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject currentPerson;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentPerson = new JSONObject(json);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // so I can access the name like&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name = currentPerson.getString("name");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<String> alsoKnownAs= new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //use getJSONArray to get the list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONArray arrayKnownAs = currentPerson.getJSONArray("alsoKnownAs");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0, l = arrayKnownAs.length(); i < l; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//This is where I was getting it wrong, i needed to use getString to access list items&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alsoKnownAs.add(arrayKnownAs.getString(i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Person thisPerson =&nbsp; new Person(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //I instantiate person object here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return thisPerson;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (org.json.JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// error&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java