【问题标题】:storing json parsed values as string in array将 json 解析值作为字符串存储在数组中
【发布时间】:2013-05-01 11:00:22
【问题描述】:

我有一个 json 对象,它作为字符串被收集到一个函数中。
它包含数组

{"officer_name":"V. M. ARORA"}{"officer_name":"Dr. C. P. REDDY"}{"officer_name":"ARTI CHOWDHARY"}{"officer_name":"JAGDISH 辛格"}

这是安卓代码

public void func4(View view)throws Exception
{
    AsyncHttpClient client = new AsyncHttpClient();
    RequestParams rp = new RequestParams();
    rp.put("pLat", "SELECT officer_name FROM iwmp_officer");

    client.post("http://10.0.2.2/conc5.php", rp, new AsyncHttpResponseHandler() {
        public final void onSuccess(String response) {
            // handle your response here
            ArrayList<String> User_List = new ArrayList<String>();
            try { 
                 /* here I need output in an array,
                 and only names not the "officer_name" */
            } catch (Exception e) {
                tx.setText((CharSequence) e);
            }

            //tx.setText(User_List.get(1));
        }

        @Override
        public void onFailure(Throwable e, String response) {
            // something went wrong
            tx.setText(response);
        }               
    });
}

我上面显示的输出是字符串,需要在数组中获取。请帮忙!

【问题讨论】:

  • 对 JSON 做一些研究。 JSONArray 会做你需要的。您上面显示的 Json 也是无效的

标签: android json


【解决方案1】:

如果你得到的输出是这样的。

String outputJson=[{"officer_name":"V. M. ARORA"}{"officer_name":"Dr. C. P. REDDY"}{"officer_name":"ARTI CHOWDHARY"}{"officer_name":"JAGDISH SINGH"}]

然后它是一个 JSON 数组。 您可以将其解析为

JsonArray array=new JsonArray(outputJson);

然后使用循环这个json数组

for(JsonObject jsonObj in array){

String officerName=[jsonObj getString("officer_name");

}

你可以使用类似上面的代码。提到的代码在语法上是不正确的,但在概念上是正确的。你可以继续这样做。

【讨论】:

    【解决方案2】:
    List < String > ls = new ArrayList< String >();
    JSONArray array    = new JSONArray( response );
    
    for (int  i = 0; i < array.length() ; i++ ) {
        JSONObject obj = array.getJSONObject(Integer.toString(i));
    
        ls.add(obj.getString("officer_name"));
    }
    

    这样就可以了

    【讨论】:

    【解决方案3】:
    try {
                        JSONArray array= new JSONArray(response);
                        //array = new JSONArray(response);
                          for (int i = 0; i < array.length(); i++) { 
                              //JSONObject obj = response.getJSONArray(i);
                              JSONObject jsonLineItem = (JSONObject) array.getJSONObject(i);
                              String name_fd = jsonLineItem.getString("officer_name");
                              User_List.add(jsonLineItem.getString("officer_name"));
                              Log.d("JSONArray", name_fd+"   " +name_fd);
    
                          }
    
    
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        tx.setText( e.toString());
                    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-23
      相关资源
      最近更新 更多