Android 迭代从多维数组转换的JSON数据
这很好用 我使用此代码块读取从Android 迭代从多维数组转换的JSON数据,android,arrays,json,multidimensional-array,apache-httpclient-4.x,Android,Arrays,Json,Multidimensional Array,Apache Httpclient 4.x,这很好用 我使用此代码块读取从FetchData.php文件获取的json数据;我的代码如下所示: HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://10.0.2.2/mydummyproject/FetchData.php"); TextView textView = (TextView)findViewById(R.id.textView1); try {
FetchData.php
文件获取的json数据;我的代码如下所示:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/mydummyproject/FetchData.php");
TextView textView = (TextView)findViewById(R.id.textView1);
try {
HttpResponse response = httpclient.execute(httppost);
String jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
JSONObject object = new JSONObject(jsonResult);
String name = object.getString("name");
String verion = object.getString("version");
textView.setText(name + " - " + verion);
}
catch (JSONException e) {
e.printStackTrace();
}
//Loop over each json object
for data in JSONObject:
// Print name and version
textView.setText(data.name + " - " + data.version)
来自我的PHP文件的数据FetchData.PHP
{"name":"John Doe","version":"Android 4.4"}
[{"name":"John Doe","version":"Android 4.4"}, {"name":"Seliana Gomez","version":"Android 4.1"}, {"name":"Nerdy Trumph","version":"Android 4.4"}]
问题
我还有另一种类型的数据来自FetchDataTwo.php
{"name":"John Doe","version":"Android 4.4"}
[{"name":"John Doe","version":"Android 4.4"}, {"name":"Seliana Gomez","version":"Android 4.1"}, {"name":"Nerdy Trumph","version":"Android 4.4"}]
现在,上面的一个也是我通过在PHP
文件中执行json\u encode($多维数组)
得到的json数据
问题
如何从json循环这个多维编码数组。所以我可以这样迭代(json数据对json数据):
[注意:下面是我需要获取的数据,我不想以显示的方式排列。这只是为了清晰和举例]
Name | Version
John Doe | Android 4.4
Seliana Gomez | Android 4.1
Nerdy Trumph | Android 4.4
看起来您所要做的就是迭代
JSONArray
try {
JSONArray array = new JSONArray(inputString);
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObject = array.optJSONObject(i);
// Json Object handling...
}
} catch (JSONException e) {
// handle
}
然后使用Gson
实例自动解析字符串
Gson gson = new Gson();
NameVersionPair[] result = gson.fromJson(inputString, NameVersionPair[].class);