0

您好,有没有办法可以将此 json 对象转换为两个数组?我已经尝试了下面的代码,它工作正常,但是我无法将 json 对象转换为两个数组,一个名为“id”,另一个数组名为“name”。我该怎么办。这就是我已经走了多远

 int[] id= {};
 String[] names={};

myfunction = new myFunctions();
    json = userfunction.petitions();

    try {
        if (json.get(KEY_SUCCESS) != null) {
            String rs = json.getString(KEY_SUCCESS);
            if (Integer.parseInt(rs) == 1) {
                inserting ids into id array here....
                inserting names into names array here..
            }

        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

json对象是

10-25 02:08:54.325: E/JSON(406): {"success":1,"petitions":[{"id":"1","name":"Has        Jennifer Musisi done great work for Kampala","timestamp":"2013-10-24 14:32:58"},{"id":"2","name":"Do lecturers deserve 100% salary increase","timestamp":"2013-10-24 14:32:58"},{"id":"3","name":"Olara Otunu should get married","timestamp":"2013-10-24 14:33:53"},{"id":"4","name":"Teachers deserve 30 not 20 salary rise","timestamp":"2013-10-24 14:33:53"},{"id":"5","name":"Prostitution should be banned","timestamp":"2013-10-24 14:33:53"}]}
4

2 回答 2

0

petitions是一个 JSON 数组,因此您可以创建一个 JSONArray 对象,捕获值,然后从数组中获取字符串。

例如:

id[0] = myArray[0].getString("id");
names[0] = myArray[0].getString("names");

只需将其放入 for 循环中即可myArray.length()

于 2013-10-24T23:52:22.393 回答
0
JSONArray jar=json.getJSONArray("petitions");
 int len=jar.length();
for(int ctr=0;ctr<len;ctr++)
{
 JSONObject job =  jar.getI(ctr);   


}

希望这可以帮助。

于 2013-10-25T02:26:16.817 回答