gpt4 book ai didi

android - 从 JSON Android 获取数据

转载 作者:行者123 更新时间:2023-11-29 19:20:11 25 4
gpt4 key购买 nike

我正在尝试将“Designacao”文本检索到 ListView ,问题是我在其他数组(没有名称 BTW)中有一个数组(paises),我该怎么做?

JSON:

[
{
paises: [
{
K_PAIS: "mz",
K_CONTINENTE: 2,
K_LINGUAGEM: "pt",
Designacao: "Moçambique",
URL_IMAGE_LARGE: "/Images/large-flags/mz-large.jpg",
URL_IMAGE_SMALL: "/Images/flags/mz.png",
Coord_LAT: -18.4335022,
Coord_LONG: 30.9779447,
Coord_Zoom: 6,
Status: "OK"
},
{
K_PAIS: "ma",
K_CONTINENTE: 2,
K_LINGUAGEM: "pt",
Designacao: "Marrocos",
URL_IMAGE_LARGE: "/Images/large-flags/ma-large.jpg",
URL_IMAGE_SMALL: "/Images/flags/ma.png",
Coord_LAT: 31.7252847,
Coord_LONG: -11.5777195,
Coord_Zoom: 6,
Status: "OK"
},
{
K_PAIS: "dz",
K_CONTINENTE: 2,
K_LINGUAGEM: "pt",
Designacao: "Argélia",
URL_IMAGE_LARGE: "/Images/large-flags/dz-large.jpg",
URL_IMAGE_SMALL: "/Images/flags/dz.png",
Coord_LAT: 27.7538925,
Coord_LONG: -7.3521369,
Coord_Zoom: 5,
Status: "OK"
}
],
K_CONTINENTE: 2,
K_LINGUAGEM: "pt",
Designacao: "África",
URL_IMAGE_LARGE: "/Images/large-flags/world-large.png",
URL_IMAGE_SMALL: "/Images/flags/world.png",
Coord_LAT: 3.6906886,
Coord_LONG: 12.63657,
Coord_Zoom: 3,
Data_Criacao: "2017-03-01T00:00:00",
Data_Alteracao: "2017-03-01T00:00:00",
Status: "OK"
},
{
paises: [
{
K_PAIS: "us",
K_CONTINENTE: 3,
K_LINGUAGEM: "pt",
Designacao: "Estados Unidos da América",
URL_IMAGE_LARGE: "/Images/large-flags/us-large.jpg",
URL_IMAGE_SMALL: "/Images/flags/us.png",
Coord_LAT: 37.09024,
Coord_LONG: -95.712891,
Coord_Zoom: 4,
Status: "OK"
},
{
K_PAIS: "mx",
K_CONTINENTE: 3,
K_LINGUAGEM: "pt",
Designacao: "México",
URL_IMAGE_LARGE: "/Images/large-flags/mx-large.jpg",
URL_IMAGE_SMALL: "/Images/flags/mx.png",
Coord_LAT: 23.3853163,
Coord_LONG: -111.5655016,
Coord_Zoom: 5,
Status: "OK"
}
],
K_CONTINENTE: 3,
K_LINGUAGEM: "pt",
Designacao: "América do Norte",
URL_IMAGE_LARGE: "/Images/large-flags/world-large.png",
URL_IMAGE_SMALL: "/Images/flags/world.png",
Coord_LAT: 54.5259614,
Coord_LONG: -105.2551187,
Coord_Zoom: 4,
Data_Criacao: "2017-03-01T00:00:00",
Data_Alteracao: "2017-03-01T00:00:00",
Status: "OK"
},
{},
{},
{},
{}
]

这是 mainactivity 上 java 代码的重要部分:

                JSONArray jsonArray = new JSONArray();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray paises = jsonObj.getJSONArray("paises");

//loop
for (int h = 0; h < paises.length(); h++) {
JSONObject c = paises.getJSONObject(h);

String K_PAIS = c.getString("K_PAIS");
String Designacao = c.getString("Designcao");

HashMap<String, String> pais = new HashMap<>();

pais.put("K_PAIS", K_PAIS);
pais.put("Designacao", Designacao);

listaPaises.add(pais);
}

}

我的代码主要基于:http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

编辑内容:

@Override
protected Void doInBackground(Void... params) {
HttpHandler sh = new HttpHandler();

String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from URL: " + jsonStr);

if (jsonStr != null) {
try {

JSONArray array = new JSONArray(jsonStr);
for (int i = 0; i < array.length(); i++) {

JSONObject jsonObject = array.getJSONObject(i);

JSONArray paises = jsonObject.optJSONArray("paises");

if (paises != null) {
for (int j = 0; j < paises.length(); j++) {
JSONObject jsonObject1 = paises.getJSONObject(j);
System.out.println(jsonObject1.optString("Designacao"));
String K_PAIS = jsonObject1.getString("K_PAIS");
String Designacao = jsonObject1.getString("Designcao");

HashMap<String, String> pais = new HashMap<>();

pais.put("K_PAIS", K_PAIS);
pais.put("Designacao", Designacao);

listaPaises.add(pais);
}
}
System.out.println(jsonObject.optString("Designacao"));
}


} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Json parsin error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Couldn't get json from server. Check LogCat for possible errpr!", Toast.LENGTH_LONG).show();
}
});
}
return null;
}

最佳答案

问题

1.) 您的响应是 JSONArray 而不是 JSONObject

new JSONObject(jsonStr); // error

2.) 您的一些 JSONObjects 是空的,因此您需要检查您的 JSONObject 是否包含任何 paises 数组。

3.) 嵌套数组意味着嵌套循环

        // JSONArray response
JSONArray array = new JSONArray(jsonStr);

// traverse your array objects
for (int i = 0; i < array.length(); i++) {
// fetch object one by one using index i
JSONObject jsonObject =array.getJSONObject(i);

// optJSONArray will give you null if there is no paises jsonArray
JSONArray paises =jsonObject.optJSONArray("paises");

if (paises!=null) { // nullity check
for (int j = 0; j < paises.length(); j++) { // nested loop for paises array
// fetch paises array objects
JSONObject jsonObject1 = paises.getJSONObject(j);
// ahh ! this is what you were looking for
// optString give you "" empty string if there is no value
System.out.println(jsonObject1.optString("Designacao"));
}
}
// fetch value which is outside paises array
System.out.println(jsonObject.optString("Designacao"));
}

输出:

Moçambique
Marrocos
Argélia
Ã?frica
Estados Unidos da América
México
América do Norte

注意:避免一些奇怪的?等系统字体问题

关于android - 从 JSON Android 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42678245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com