如何从JSONObject填充String数组以在ListView中使用

时间:2012-11-26 07:30:21

标签: java android

我有一个android / java任务,我希望将JSON值转换为String数组以显示在ListView中,我不知道从哪里开始?谢谢。

private String[] values;
...
// this is what is returned from the web server (Debug view)
// jObj = {"success":1,"0":"Mike","1":"message 1","2":"Fred","3":"message 2","4":"John","5":"message 3"};

try {
   if (jObj.getInt("success") == 1) {
      .
      // what i'm trying to do here is iterate thru JObj and assign values to the 
      // values array to populate the ArrayAdapter so that the ListView displays this:
      //
      // Mike: Message 1
      // Fred: Message 2
      // John: Message 3
      //
      .
      this.setListAdapter(new ArrayAdapter<String>(
         this, android.R.layout.simple_list_item_1, android.R.id.text1, values));
      ListView listView = getListView();
   }
}
catch (JSONException e) {
   Log.e(TAG, e.toString());
}

3 个答案:

答案 0 :(得分:1)

使用ArrayList代替Array添加从Json Obejct检索的值,然后将ListView的ArrayList设置为数据源。将您的代码更改为:

 ArrayList<String> array_list_values = new ArrayList<String>();
try {
       if (jObj.getInt("success") == 1) {

        array_list_values.add(jObj.getString("0"));
        array_list_values.add(jObj.getString("1"));
        array_list_values.add(jObj.getString("2"));
        array_list_values.add(jObj.getString("3"));
        array_list_values.add(jObj.getString("4"));
        array_list_values.add(jObj.getString("5"));
        this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, array_list_values));
                ListView listView = getListView();
            }
     }
catch (JSONException e) {
      Log.e(TAG, e.toString());
  }

编辑: 如果消息数量不总是相同,则可以是1到更大的数字,那么您可以将JsonObject迭代为:

 ArrayList<String> array_list_values = new ArrayList<String>();
try {
       if (jObj.getInt("success") == 1) {

        Iterator iter = jObj.keys();
        while(iter.hasNext()){
        String key = (String)iter.next();
        String value = jObj.getString(key);
        array_list_values.add(value);
        }


        this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, array_list_values));
                ListView listView = getListView();
            }
     }
catch (JSONException e) {
      Log.e(TAG, e.toString());
  }

答案 1 :(得分:0)

你应该做那样的事情并使用令人敬畏的GSON库:

InputStream source = retrieveStream(url);
Gson gson = new Gson();
Reader reader = new InputStreamReader(source);
APIResponse response = gson.fromJson(reader, APIResponse.class);

使用:

Public class APIResponse{

public String 0;
public String 1;
public String 2;
public String 3;
public String 4;
public String 5;
public String succes;

}

但这不起作用,因为你需要为变量找到另一个0,1,2,3,4,5的名称。

请在服务器端更改此内容

答案 2 :(得分:0)

JSONObject jsonObject = new JSONObject(jsonString);
String value0 = jsonObject.getString("0");  

或 在for循环中

String tempArray[] = new String[5];

只是做

for(int i=0;condition;i++){
    tempArray[i] = jsonObject.getString(String.ValueOf(i));
}

并将数组传递给适配器