gpt4 book ai didi

java - 如何在 Java 和 Android 中从 JSON 读取想要的数据

转载 作者:行者123 更新时间:2023-12-02 03:34:22 25 4
gpt4 key购买 nike

几天前我问了一个类似的问题,关于如何从 AngularJS 中的 JSON 文件中读取所需的数据,但我要在 Android 中的 java 中完成这项工作,所以我在读取和记录这样的 JSON 文件时遇到问题, :

{
"results" : [
{
"address_components" : [
{
"long_name" : "277",
"short_name" : "277",
"types" : [ "street_number" ]
},
{
"long_name" : "Bedford Avenue",
"short_name" : "Bedford Ave",
"types" : [ "route" ]
},
{
"long_name" : "Williamsburg",
"short_name" : "Williamsburg",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Brooklyn",
"short_name" : "Brooklyn",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Kings County",
"short_name" : "Kings County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "11211",
"short_name" : "11211",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "277 Bedford Ave, Brooklyn, NY 11211, USA",
"geometry" : {
"location" : {
"lat" : 40.714232,
"lng" : -73.9612889
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 40.7155809802915,
"lng" : -73.9599399197085
},
"southwest" : {
"lat" : 40.7128830197085,
"lng" : -73.96263788029151
}
}
},
"place_id" : "ChIJd8BlQ2BZwokRAFUEcm_qrcA",
"types" : [ "street_address" ]
},
],
"status" : "OK"
}

我知道在 Java/android 中我们有 2 种类型(JSON 数组和 JSON 对象,它们用 [ 和 { )表示。我只需要城市名称,而不是所有这些数据。我如何记录只想要的对象行。

我尝试了这段代码,但不起作用:

protected String doInBackground(String... params) {


HttpURLConnection connection = null;
BufferedReader reader = null;

try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
String data = "";
while ((line = reader.readLine()) != null) {
buffer.append(line+"\n");
Log.d("Response: ", "> " + line);
try {
JSONObject jsonRootObject = new JSONObject(line);


JSONArray jsonArray = jsonRootObject.optJSONArray("results");
for(int i=0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.optString("formatted_address").toString();


data = name;
}

} catch (JSONException e) {e.printStackTrace();}

}

return data.toString();


} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}

-------------- 编辑:我使用了这段代码:

JSONObject  jsonRootObject = new JSONObject(newData);

JSONArray jsonArray = jsonRootObject.optJSONArray("results");
for(int i=0; i <=1 ; i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);

data = jsonObject.getString("address_components");
}

现在我有另一个像这样的 json 字符串:

        [
{
"long_name" : "277",
"short_name" : "277",
"types" : [ "street_number" ]
},
{
"long_name" : "Bedford Avenue",
"short_name" : "Bedford Ave",
"types" : [ "route" ]
},
{
"long_name" : "Williamsburg",
"short_name" : "Williamsburg",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Brooklyn",
"short_name" : "Brooklyn",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
}]

我如何访问对象(n)的“long_name”键的值?

最佳答案

将您的 try block 更改为

        while ((line = reader.readLine()) != null) {
buffer.append(line+"\n");
}

Log.d("Response: ", "> " + line);
try {
JSONObject jsonRootObject = new JSONObject(line);

JSONArray jsonArray = jsonRootObject.optJSONArray("results");
for(int i=0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
data = jsonObject.getString("formatted_address")

}

} catch (JSONException e) {e.printStackTrace();}
return data;

现在您将拥有有效的字符串到 JSONObject 转换,因为之前您在 while 循环中解析不完整的字符串,这不是有效的 json。

关于java - 如何在 Java 和 Android 中从 JSON 读取想要的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37617246/

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