我是 JSON 的新手,正在尝试这个教程: Http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/#comments
我对 JSON、 C 语言、 Java 和 Android 都是新手,但我正在学习。本教程使用我称之为命名数组的东西,但是我将在 android 项目中使用的所有 JSON 都将使用没有命名数组的简单表行。我正在使用的 JSON 和本教程中的地震 JSON 的示例如下。
本教程迭代地震数组,并使用以下代码将其转换为 JAVA 散列表:
JSONArray earthquakes = json.getJSONArray("earthquakes");
for(int i=0;i<earthquakes.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = earthquakes.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name", "Earthquake name:" + e.getString("eqid"));
map.put("magnitude", "Magnitude: " + e.getString("magnitude"));
mylist.add(map);
}
我的问题是,如果我的 JSON 如下所示简单,我如何使用 json.getJSONArray("")
?我可以转换其余的代码,我只需要知道如何加载该 JSON 使用的 getJSONArray("strJsonArrayName")
,如果我没有一个 strJsonArrayName
。
我的 JSON (未命名数组)
[
{
"cnt":1,
"name":"American",
"pk":7
},
{
"cnt":2,
"name":"Celebrities",
"pk":3
},
{
"cnt":1,
"name":"Female",
"pk":2
},
{
"cnt":1,
"name":"Language",
"pk":8
},
{
"cnt":1,
"name":"Male",
"pk":1
},
{
"cnt":1,
"name":"Region",
"pk":9
}
]
教程的 JSON (命名数组)
{
"earthquakes":[
{
"eqid":"c0001xgp",
"magnitude":8.8,
"lng":142.369,
"src":"us",
"datetime":"2011-03-11 04:46:23",
"depth":24.4,
"lat":38.322
},
{
"eqid":"c000905e",
"magnitude":8.6,
"lng":93.0632,
"src":"us",
"datetime":"2012-04-11 06:38:37",
"depth":22.9,
"lat":2.311
},
{
"eqid":"2007hear",
"magnitude":8.4,
"lng":101.3815,
"src":"us",
"datetime":"2007-09-12 09:10:26",
"depth":30,
"lat":-4.5172
},
{
"eqid":"c00090da",
"magnitude":8.2,
"lng":92.4522,
"src":"us",
"datetime":"2012-04-11 08:43:09",
"depth":16.4,
"lat":0.7731
},
{
"eqid":"2007aqbk",
"magnitude":8,
"lng":156.9567,
"src":"us",
"datetime":"2007-04-01 18:39:56",
"depth":10,
"lat":-8.4528
},
{
"eqid":"2007hec6",
"magnitude":7.8,
"lng":100.9638,
"src":"us",
"datetime":"2007-09-12 21:49:01",
"depth":10,
"lat":-2.5265
},
{
"eqid":"a00043nx",
"magnitude":7.7,
"lng":100.1139,
"src":"us",
"datetime":"2010-10-25 12:42:22",
"depth":20.6,
"lat":-3.4841
},
{
"eqid":"2010utc5",
"magnitude":7.7,
"lng":97.1315,
"src":"us",
"datetime":"2010-04-06 20:15:02",
"depth":31,
"lat":2.3602
},
{
"eqid":"2009mebz",
"magnitude":7.6,
"lng":99.9606,
"src":"us",
"datetime":"2009-09-30 08:16:09",
"depth":80,
"lat":-0.7889
},
{
"eqid":"2009kdb2",
"magnitude":7.6,
"lng":92.9226,
"src":"us",
"datetime":"2009-08-10 17:55:39",
"depth":33.1,
"lat":14.0129
}
]
}
在本教程中,基于@M Γ LL 和@Cody Caughlan 的答案,我能够将 JSONFunctions.getJSONFromURL 重新格式化为 JSONArray 而不是 JSONObject。这是我修改后的工作代码,谢谢!
public class JSONfunctions {
public static JSONArray getJSONfromURL(String url){
InputStream is = null;
String result = "";
JSONArray jArray = null;
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
jArray = new JSONArray(result);
return jArray;
}
}