为什么我们要在解析 JSON 或网络服务响应的情况下显式输入 Java?
Why shall we Explicitly cast in Java in case of parsing JSON or webservice responses?
我正在尝试使用 Java 中的 org.JSON 解析来自 Google 地理编码 API 的 Json 回复。根据 API 规范,回复流是 JSONObject 或 JSONArray(直到这里没有问题)。
Q1:但每次,我都必须显式转换它们(请参阅最后一行;如果我不这样做,则会出现编译器错误)。它是 API 的限制还是来自此类 Web 服务的任何此类流的限制?如果可能,请解释为什么我们必须显式地转换 Java。我们必须这样做的所有情况是什么?
Q2:我是否可以直接从 Web 服务的回复流构建 JSON 树,而不是先将其读入缓冲区对象(这里我使用了字符串缓冲区),然后再构建 JSON 树.
参考:
Google 的地理编码库
https://developers.google.com/maps/documentation/geocoding/
- 示例
- http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway
- 输入部分地址
输出可能的地址匹配列表
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public static void main(String[] args) throws JSONException {
String address = getUserInput(); //assert address = "1602+Amphitheatre+Parkway"
String URLString = "http://maps.googleapis.com/maps/api/geocode/json?address=";
String results = fetchSuggestions(URLString, address); // This has fetched entire reply from Google GeoCode API
// parse the Result String to JSON
JSONObject myJSONResult = new JSONObject(results);
for (int i = 0; i <((JSONArray) myJSONResult.get("results")).length(); i++)
System.out.println(((JSONObject) ((JSONArray) myJSONResult.get("results")).get(i)).get("formatted_address"));
}
Q1:有一些方法可以为您进行类型转换(只需阅读 API 文档 - http://www.json.org/java/):
JSONArray arr = myJSONResult.getJSONArray("results");
JSONObject obj = arr.getJSONObject(i);
您需要类型转换,因为 Java(通常,任何 OO 语言)不能有两个具有相同签名(名称和参数)和不同 return 类型的方法,它们不在一个层次结构树。因此,方法 get
可以 return JSONObject
或 JSONArray
,但是不可能有一种方法 return 两种类型而不引用它们的共同父级 - Object
。它可以通过另一个 API 设计来解决,其中一个 class 用于数组和对象,但它也不方便。
Q2:是的,但是使用 javax.json
- http://docs.oracle.com/javaee/7/api/javax/json/package-summary.html. (you need to download and install any implementation and hack into HTTP processing to get HTTP-body stream and pass to library methods: http://docs.oracle.com/javaee/7/api/javax/json/Json.html).
我正在尝试使用 Java 中的 org.JSON 解析来自 Google 地理编码 API 的 Json 回复。根据 API 规范,回复流是 JSONObject 或 JSONArray(直到这里没有问题)。
Q1:但每次,我都必须显式转换它们(请参阅最后一行;如果我不这样做,则会出现编译器错误)。它是 API 的限制还是来自此类 Web 服务的任何此类流的限制?如果可能,请解释为什么我们必须显式地转换 Java。我们必须这样做的所有情况是什么?
Q2:我是否可以直接从 Web 服务的回复流构建 JSON 树,而不是先将其读入缓冲区对象(这里我使用了字符串缓冲区),然后再构建 JSON 树.
参考: Google 的地理编码库
https://developers.google.com/maps/documentation/geocoding/
- 示例
- http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway
- 输入部分地址
输出可能的地址匹配列表
import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public static void main(String[] args) throws JSONException { String address = getUserInput(); //assert address = "1602+Amphitheatre+Parkway" String URLString = "http://maps.googleapis.com/maps/api/geocode/json?address="; String results = fetchSuggestions(URLString, address); // This has fetched entire reply from Google GeoCode API // parse the Result String to JSON JSONObject myJSONResult = new JSONObject(results); for (int i = 0; i <((JSONArray) myJSONResult.get("results")).length(); i++) System.out.println(((JSONObject) ((JSONArray) myJSONResult.get("results")).get(i)).get("formatted_address"));
}
Q1:有一些方法可以为您进行类型转换(只需阅读 API 文档 - http://www.json.org/java/):
JSONArray arr = myJSONResult.getJSONArray("results");
JSONObject obj = arr.getJSONObject(i);
您需要类型转换,因为 Java(通常,任何 OO 语言)不能有两个具有相同签名(名称和参数)和不同 return 类型的方法,它们不在一个层次结构树。因此,方法 get
可以 return JSONObject
或 JSONArray
,但是不可能有一种方法 return 两种类型而不引用它们的共同父级 - Object
。它可以通过另一个 API 设计来解决,其中一个 class 用于数组和对象,但它也不方便。
Q2:是的,但是使用 javax.json
- http://docs.oracle.com/javaee/7/api/javax/json/package-summary.html. (you need to download and install any implementation and hack into HTTP processing to get HTTP-body stream and pass to library methods: http://docs.oracle.com/javaee/7/api/javax/json/Json.html).