解析JSON数组而不是JSON对象

时间:2015-10-20 23:22:32

标签: java android arrays json

任何帮助都会非常感激......

我对android和java相当新。我之前构建了一个应用程序,它将JSON对象中的JSON数组解析为listview。我在新应用程序中需要做的是解析一个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对象,如我的第二个示例所示吗?

1 个答案:

答案 0 :(得分:0)

这应该有效:

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方法以对应字符串响应。