解析JSON阵列,而不是JSON对象

解析JSON阵列,而不是JSON对象

问题描述:

任何帮助,将不胜感激......解析JSON阵列,而不是JSON对象

我是相当新的Android和Java的。我之前已经构建了一个应用程序,它将JSON对象中的JSON数组解析为列表视图。我在新应用程序中需要做的是解析JSON对象中未包含的JSON数组,并且无法弄清楚如何修改我的代码。

Java Activity: 
public class StationListActivity extends AppCompatActivity { 


ListView mainListView; 
JSONAdapter mJSONAdapter; 



private static final String QUERY_URL = "url.json"; 
ProgressDialog mDialog; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_station_list); 




    // Access the ListView 
    mainListView = (ListView) findViewById(R.id.list); 


    // Create a JSONAdapter for the ListView 
    mJSONAdapter = new JSONAdapter(this, getLayoutInflater()); 

    // Set the ListView to use the ArrayAdapter 
    mainListView.setAdapter(mJSONAdapter); 

    mDialog = new ProgressDialog(this); 
    mDialog.setMessage("Searching"); 
    mDialog.setCancelable(false); 

    // Create a client to perform networking 
    AsyncHttpClient client = new AsyncHttpClient(); 

    // Show ProgressDialog to inform user that a task in the background is  occurring 
    mDialog.show(); 




    // Have the client get a JSONArray of data 
    // and define how to respond 
    client.get(QUERY_URL, new JsonHttpResponseHandler() { 

       @Override 
       public void onSuccess(JSONObject jsonObject) { 

        // Dismiss the ProgressDialog 
        mDialog.dismiss(); 

        // update the data in your custom method. 
        mJSONAdapter.updateData(jsonObject.optJSONArray("-Here I  would have JSON Object name which contained JSON Array-")); 
       } 

       @Override 
       public void onFailure(int statusCode, Throwable throwable,  JSONObject error) { 

        // Dismiss the ProgressDialog 
        mDialog.dismiss(); 

        // Display a "Toast" message 
        // to announce the failure 
        Toast.makeText(getApplicationContext(), "Network error,  please close app and try again", Toast.LENGTH_LONG).show(); 

        // Log error message 
        // to help solve any problems 
        Log.e("applog", statusCode + " " +  throwable.getMessage()); 
       } 
      } 
    ); 

} 

例JSON:

[ 
    { 
    "name": "Silver", 
    "id": 12, 
    "number": 34 
    } 
] 

。 。 。

例JSON我知道如何处理:

{"title": 
    [ 
     { 
     "name": "Silver", 
     "id": 12, 
     "number": 34 
     } 
    ] 
} 

它可能不是最干净的解决办法,但将有可能增加一些Java来检索JSON分配给一个JSON对象作为我的第二个例子?

+0

因此,您习惯于使用JSON对象,其中键是名称,值是JSON对象的JSON数组,对吗?你所要做的就是通过迭代JSONArray中的每个元素来访问你现在得到的json数组响应中的每个JSON对象。如有必要,我可以给你一个例子 –

+0

如果可以的话,那将会很棒。 – dmca

+0

基本上所有需要的就是调用新的JSONArray(String jsonFile),然后用for循环遍历数组。 – geokavel

这应该工作:

client.get("https://www.google.com", new AsyncHttpResponseHandler() { 

@Override 
public void onSuccess(String response) { 
    JSONArray result = new JSONArray(response); 
    for(int i = 0; i < result.length(); ++i){ 
     JSONObject obj = result.getJSONObject(i); 
     //do something with obj 
    } 
} 

我敢肯定这是你在找什么,请注意我修改了的onSuccess方法用细绳响应对应。

+0

感谢您的回答。我是否正确地说,这将依次返回数组中的单个对象?我明白这个价值,唯一的事情就是我的代码被写入接受整个数组,并且有一个JSON适配器将会使列表视图膨胀。 有什么办法让我的第二个例子的上述格式的数组? – dmca