Android:如何访问JSONObject

时间:2017-09-14 06:42:38

标签: android json

我是Android新手,我尝试了很多选项来访问从API调用返回的import { BrowserRouter as Router, } from 'react-router-dom' // Other Imports ... return ( <Router> <div className="index"> <Nav /> <!-- In this component you have <Links> --> <div className="container"> <Routes /> </div> </div> </Router> ); ,但我无法成功,因为我寻找的任何解决方案对我都不起作用。

我想要的是访问JSONObject并保留JSONObject&amp;数组中的Id。然后在Name中填充名称。如何正确访问AutoCompleteTextView。请帮我解决一下这个。我被困在这一天超过一天。

以下是我的代码处理JSONObject。

JSONObject

以下是我@Override public void processFinish(JSONObject output) { Toast.makeText(MainActivity.this,"ProcessFinish",Toast.LENGTH_SHORT).show(); allStations = output; if(output != null){ Toast.makeText(MainActivity.this,output.toString(),Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(MainActivity.this," Connection Failed!",Toast.LENGTH_SHORT).show(); } }

的示例输出
JSONObject

3 个答案:

答案 0 :(得分:1)

试试这个

try {
JSONObject obj= output.getJSONObject("RESULTS");

JSONArray dataArray= obj.getJSONArray(“stationList“);

for(int i=0;i<dataArray.length();i++)
{

    JSONObject object1=dataArray.getJSONObject(i);
    Strind id = object1.getString("stationID");
}
} catch (JSONException e) {

    e.printStackTrace();

}

在此代码中输出是您的 JSONObject 结果

答案 1 :(得分:0)

试试这个

try {
       JSONObject jsonObject = new JSONObject("response");
       boolean status= jsonObject.getBoolean("SUCCESS");
       String MESSAGE= jsonObject.getString("MESSAGE");
       String NOFRESULTS= jsonObject.getString("NOFRESULTS");
       String STATUSCODE= jsonObject.getString("STATUSCODE");


       JSONObject obj=jsonObject.getJSONObject("RESULTS");
       JSONArray jsonarray = obj.optJSONArray("stationList");
       for (int i = 0; i < jsonarray.length(); i++){
            JSONObject json_data = jsonarray.getJSONObject(i);
            Log.e("stationCode",json_data.getString("stationCode"));
            Log.e("stationID",json_data.getString("stationID"));
            Log.e("stationName",json_data.getString("stationName"));
       }
     } catch (JSONException e) {
            e.printStackTrace();
   }

答案 2 :(得分:0)

您是否尝试过使用JSON数组?例如,您可以使用此方法进行存储:

JSONObject wgroup = new JSONObject();   //FINAL json object 

try { //put initial data
    wgroup.put("id", "2");
    wgroup.put("user", "someone");
    wgroup.put("stime", "2017-02-06 16:30:13");
    wgroup.put("etime", "2017-02-06 19:30:13");
    wgroup.put("real_dur", 3600); 

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

JSONArray jsonArray1 = new JSONArray(); //Create an array to store ALL Variables

for (int y=0; y< your_array.length ; y++ ){ //loop through your information array 
JSONObject output = new JSONObject();  //CREATE a json object to put 1 workout
try {
    wgroup.put("id", "2");
    wgroup.put("name", "sam");
    wgroup.put("age", "3");
    wgroup.put("gender", "male");
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
JSONArray jsonArray2 = new JSONArray();  //CREATE a json array to put 1 array

jsonArray1.put(output);  //insert this OBJECT into the ARRAY
}
wgroup.put(jsonArray1);//insert the workouts ARRAY into the original object