如何在Android中解析多个Json值?

时间:2017-06-20 13:57:40

标签: android json android-volley android-json

我尝试使用Volley库多次这样做。

JSON值的链接:here

我的Android代码解析值:here

public class GetUSGSjson extends AppCompatActivity
{
    GridView jsonGridView;
    ListView jsonListView;
    Button jsonGetBtn;
    RequestQueue USGSrequestqueue;
    String USGSurl = "https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2016-01-01&endtime=2016-01-31&minmag=6&limit=10";
    ArrayList<String> mArrayList;
    ArrayList<String>FK;
    JsonObjectRequest USGSjsonObjectReq;
    JSONObject _properties_;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.json_data);

        jsonGridView = (GridView) findViewById(R.id.jsonGridView);
        jsonGetBtn = (Button) findViewById(R.id.jsonGetBtn);
        USGSrequestqueue = Volley.newRequestQueue(this);
        mArrayList = new ArrayList<>();
        FK = new ArrayList<>();
        jsonGridView.setVisibility(View.GONE);
    }

按下按钮

    public void USGSgetData(View view)
    {
        jsonGridView.setVisibility(View.VISIBLE);

        USGSjsonObjectReq = new JsonObjectRequest(Request.Method.GET,USGSurl,null,

        new Response.Listener<JSONObject>()
        {
            @Override
            public void onResponse(JSONObject response)
            {
                try {

                    JSONObject _features_ = response.getJSONObject("features");
                    JSONArray _f_ = response.getJSONArray("features");


                    for (int x = 0; x < _f_.length(); x++)
                    {
                        JSONObject currentFeature = _f_.getJSONObject(x);
                        _properties_ = currentFeature.getJSONObject("properties"); //JsonObject
                        double mag = _properties_.getDouble("mag");
                        long time = _properties_.getLong("time");
                        String place = _properties_.getString("place");

                        mArrayList.add(String.valueOf(mag));
                        mArrayList.add(String.valueOf(time));
                        mArrayList.add(place);
                        ArrayAdapter VarrayAdapter = new ArrayAdapter<>(GetUSGSjson.this
                            ,android.R.layout.simple_list_item_1, mArrayList);

                        jsonGridView.setAdapter(VarrayAdapter);

                    } catch (JSONException e)
                    {
                        e.printStackTrace();
                        Toast.makeText(GetUSGSjson.this, "Exception: "+e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error)
                {
                    Toast.makeText(GetUSGSjson.this, "onErrorResponse: "+error.getMessage(), Toast.LENGTH_LONG).show();
                }

            }
            );
            USGSrequestqueue.add(USGSjsonObjectReq);
        }
    }

2 个答案:

答案 0 :(得分:0)

试试这个,

try {
    JSONArray arr_features=response.getJSONArray("features");

    for(int i=0;i<arr_features.length();i++)
    {
        JSONObject obj=arr_features.getJSONObject(i);
        String type=obj.getString("type");
        JSONObject obj_properties=obj.getJSONObject("properties");
        String mag=obj_properties.getString("mag");
        String place=obj_properties.getString("place");
        String time=obj_properties.getString("time");
        String updated=obj_properties.getString("updated");

    }

} catch (JSONException e) {
    e.printStackTrace();
}

答案 1 :(得分:0)

我找到了解决自己问题的方法 请记住&#34; mag&#34;是形式&#34;地点&#34; &安培; &#34;类型&#34;是字符串形式&amp; &#34;时间&#34;是一个Unix时间它是形式。

try {
                            JSONArray _f_ = response.getJSONArray("features");

                            for (int x = 0; x < _f_.length(); x++)
                            {
                                JSONObject currentFeature = _f_.getJSONObject(x);
                                 _properties_ = currentFeature.getJSONObject("properties"); //JsonObject
                               double mag = _properties_.getDouble("mag");
                                long time = _properties_.getLong("time");
                                String place = _properties_.getString("place");
                                String type = _properties_.getString("type");
// To add this on to ArrayList.



mArrayList.add("Place: "+place+"\n");
    mArrayList.add("\nMagnitude: " + String.valueOf(mag)+"\n");
    mArrayList.add("\nTime: " + String.valueOf(newTime)+"\n");
    mArrayList.add("\nType: " + type+"\n");
}