如何在Android中解析多个Json值?
我试着用截击库做了多次 JSON值的链接: 我的android代码用于解析值:如何在Android中解析多个Json值?,android,json,android-volley,android-json,Android,Json,Android Volley,Android Json,我试着用截击库做了多次 JSON值的链接: 我的android代码用于解析值: 公共类GetUSGSjson扩展AppCompative活动 { GridView-jsonGridView; ListView jsonListView; 按钮jsonGetBtn; 请求队列USGSrequestqueue; 字符串USGSurl=”https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2016-01-0
公共类GetUSGSjson扩展AppCompative活动
{
GridView-jsonGridView;
ListView jsonListView;
按钮jsonGetBtn;
请求队列USGSrequestqueue;
字符串USGSurl=”https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2016-01-01&endtime=2016-01-31&minmag=6&limit=10”;
ArrayList婚姻列表;
ArrayListFK;
JsonObjectRequest-USGSjsonObjectReq;
JSONObject(属性);;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.json_数据);
jsonGridView=(GridView)findviewbyd(R.id.jsonGridView);
jsonGetBtn=(按钮)findviewbyd(R.id.jsonGetBtn);
USGSrequestqueue=Volley.newRequestQueue(this);
mArrayList=newarraylist();
FK=新的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);
}
}
public void USGSgetData(视图)
{
jsonGridView.setVisibility(View.VISIBLE);
USGSjsonObjectReq=newjsonobjectrequest(Request.Method.GET,USGSurl,null,
新的Response.Listener()
{
@凌驾
公共void onResponse(JSONObject响应)
{
试一试{
JSONObject_features_=response.getJSONObject(“features”);
JSONArray f_u=response.getJSONArray(“特性”);
对于(int x=0;x<_f_.length();x++)
{
JSONObject currentFeature=\u f_u.getJSONObject(x);
_properties\uux=currentFeature.getJSONObject(“属性”);//JsonObject
double mag=u properties_u.getDouble(“mag”);
长时间=u属性_u.getLong(“时间”);
字符串位置=properties.getString(“位置”);
mArrayList.add(String.valueOf(mag));
mArrayList.add(String.valueOf(time));
结婚名单。添加(地点);
ArrayAdapter VarrayAdapter=新的ArrayAdapter(GetUSGSjson.this
,android.R.layout.simple_list_item_1,mArrayList);
setAdapter(VarrayAdapter);
}捕获(JSONException e)
{
e、 printStackTrace();
Toast.makeText(GetUSGSjson.this,“异常:+e.getMessage(),Toast.LENGTH_LONG).show();
}
}
},
新的Response.ErrorListener()
{
@凌驾
公共无效onErrorResponse(截击错误)
{
Toast.makeText(GetUSGSjson.this,“onErrorResponse:+error.getMessage(),Toast.LENGTH_LONG.show();
}
}
);
添加(USGSjsonObjectReq);
}
}
试试这个
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();
}
试试看{
JSONArray arr_features=response.getJSONArray(“features”);
对于(inti=0;i我找到了解决自己问题的方法
记住,“mag”在double格式中,“place”和“type”在String格式中&“time”是Unix时间,它在long格式中
试试看{
JSONArray f_u=response.getJSONArray(“特性”);
对于(int x=0;x<_f_.length();x++)
{
JSONObject currentFeature=\u f_u.getJSONObject(x);
_properties\uux=currentFeature.getJSONObject(“属性”);//JsonObject
double mag=u properties_u.getDouble(“mag”);
长时间=u属性_u.getLong(“时间”);
字符串位置=properties.getString(“位置”);
字符串类型=properties.getString(“类型”);
//将其添加到ArrayList。
结婚名单。添加(“地点:“+Place+”\n”);
mArrayList.add(“\n幅值:”+String.valueOf(mag)+“\n”);
mArrayList.add(“\n时间:”+String.valueOf(newTime)+“\n”);
婚姻列表。添加(“\nType:+type+”\n”);
}
请阅读“嘿,对不起,问题中的错误。您现在可以回答吗?”?
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();
}
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");
}