试图JSON数据转换成JSON数组Android Studio中
问题描述:
你好我尝试一些JSON数据转换成一个数组,我不这样说之前,但没有对象试图JSON数据转换成JSON数组Android Studio中
http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_day.geojson
下面是代码该获取数据,并改掉其转换
public JSONArray getQaukes()
{
String url = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_day.geojson";
// Get HttpResponse Object from url.
// Get HttpEntity from Http Response Object
HttpEntity httpEntity = null;
try
{
DefaultHttpClient httpClient = new DefaultHttpClient(); // Default HttpClient
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
httpEntity = httpResponse.getEntity();
} catch (ClientProtocolException e) {
// Signals error in http protocol
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// Convert HttpEntity into JSON Array
JSONArray jsonArray = null;
if (httpEntity != null) {
try {
String entityResponse = EntityUtils.toString(httpEntity);
Log.d("Entity Response : ", entityResponse);
jsonArray = new JSONArray(entityResponse);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return jsonArray;
}
这就是我得到 http://pastebin.com/HVnx3gsq
任何人都知道我可以如何找到正确的方法来做到这一点? 感谢
答
编辑:举个例子,如果你要提取“功能”阵列,那么你应该那样做:
//First of all - create JSON Object which you are going to parse (deserialize JSON string)
JSONObject jsonObj = new JSONObject(entityResponse);
// Extract JSON array from Object
JSONArray jsonArray = jsonObj.getJSONArray("features");
你的JSON字符串中有许多不同的元素,这就是为什么你需要创建JSON对象,然后从中提取数组等等。直到最后一个标记。
"type":"FeatureCollection",
"metadata":{
"generated":1452461493000,
"url":"http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_day.geojson",
"title":"USGS Magnitude 1.0+ Earthquakes, Past Day",
"status":200,
"api":"1.1.0",
"count":128
},
"features":[
{
"type":"Feature",
"properties":{
"mag":2.07,
"place":"28km S of Gardnerville Ranchos, Nevada",
"time":1452460963440,
"updated":1452461071893,
"tz":-480,
"url":"http://earthquake.usgs.gov/earthquakes/eventpage/nc72578341",
"detail":"http://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/nc72578341.geojson",
"felt":null,
"cdi":null,
"mmi":null,
"alert":null,
"status":"automatic",
"tsunami":0,
"sig":66,
"net":"nc",
"code":"72578341",
"ids":",nn00526227,nc72578341,",
"sources":",nn,nc,",
"types":",general-link,general-link,geoserve,nearby-cities,origin,phase-data,",
"nst":8,
"dmin":0.06765,
"rms":0.11,
"gap":197,
"magType":"md",
"type":"earthquake",
"title":"M 2.1 - 28km S of Gardnerville Ranchos, Nevada"
},
"geometry":{
"type":"Point",
"coordinates":[
-119.751503,
38.6351662,
-1.8
]
},
"id":"nc72578341"
},
{
"type":"Feature",
"properties":{
"mag":2.3,
在你上面的提取物具有: “类型” - JSON值, “元数据” - JSON对象, “特征” - JSON阵列等。
我建议你寻找到JSON的语法,所以那么你就能理解如何分析数据:http://www.w3schools.com/json/json_syntax.asp
答
结果是JSONObject
不JSONArray
。
而不是JSONArray jsonArray = new JSONArray(entityResponse);
你应该有JSONObject jObj = new JSONObject(entityResponse);
感谢您对这样的快速回复,我会看看的语法和尝试将对象转换为数组。 :D – ThatFellaNick
@ThatFellaNick查看答案开头的更新。 –
感谢您的帮助,我能够获得我需要的所有数据! :D – ThatFellaNick