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

我试过多次使用Volley库。如何解析Android中的多个Json值?

的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);

}

}

回答:

试试这个的OnCllick,

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();

}

回答:

我发现这种方式来解决我自己的问题 请记住,“MAG”是双形式的“地方” &“类型”是字符串形式&“时间”是一个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");

}

以上是 如何解析Android中的多个Json值? 的全部内容, 来源链接: utcz.com/qa/265156.html

回到顶部