我正在开发一个 RESTful Android 移动客户端。我的应用程序和服务器之间的信息交换采用 JSON 格式。所以我现在有点困惑选择什么数据结构来表示 JSON 响应和数据,因为它们有很多。我刚刚停止使用 LinkedHashMap<> 但据我所知 JSON 是无序的。在互联网上,我看到人们为此使用 Map<> 或 HashMap<>。
所以问题是——为此目的最好的数据结构是什么?或者,如果没有明确的答案——我已经提到过使用数据结构的利弊。
我正在开发一个 RESTful Android 移动客户端。我的应用程序和服务器之间的信息交换采用 JSON 格式。所以我现在有点困惑选择什么数据结构来表示 JSON 响应和数据,因为它们有很多。我刚刚停止使用 LinkedHashMap<> 但据我所知 JSON 是无序的。在互联网上,我看到人们为此使用 Map<> 或 HashMap<>。
所以问题是——为此目的最好的数据结构是什么?或者,如果没有明确的答案——我已经提到过使用数据结构的利弊。
我不同意第一个答案。开发 REST 范式是为了让您使用对象而不是操作进行操作。
对我来说,最明智的方法是在客户端声明 bean 并解析 json 响应并通过它们请求。我建议使用GSON 库进行序列化/反序列化。JsonObject
/JsonArray
几乎从来都不是最好的选择。
也许如果您给出您将要使用的操作的示例,我们可能能够更准确地提供帮助。
编辑:让我也举几个 GSON 的例子。让我们使用这个线程来比较不同的库。
在大多数情况下,REST 服务与对象通信。假设您发布了产品的帖子,其中提到了商店。
{ "name": "Bread",
"price": 0.78,
"produced": "08-12-2012 14:34",
"shop": {
"name": "neighbourhood bakery"
}
}
然后,如果您声明以下 bean:
public class Product {
private String name;
private double price;
private Date produced;
private Shop shop;
// Optional Getters and setters. GSON uses reflection, it doesn't need them
// However, better declare them so that you can access the fields
}
public class Shop {
private String name;
// Optional Getters and setters. GSON uses reflection, it doesn't need them
// However, better declare them so that you can access the fields
}
您可以使用以下方法反序列化 json:
String jsonString; // initialized as you can
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setDateFormat("MM-dd-yyyy HH:mm"); // setting custom date format
Gson gson = gsonBuilder.create();
Product product = gson.fromJson(jsonString, Product.class);
// Do whatever you want with the object it has its fields loaded from the json
另一方面,您可以更轻松地序列化为 json:
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setDateFormat("MM-dd-yyyy HH:mm"); // setting custom date format
Gson gson = gsonBuilder.create();
String jsonString = gson.toJson(product);
您是在谈论从服务器请求中接收和解析 JSON 字符串吗?
为此,您可以使用:
import org.json.JSONArray;
import org.json.JSONObject;
使用这些,我从我的 POST 请求中读取我的 JSON 数组,并将结果信息存储在我项目中的 Class 对象中。
对于 JSONArray 中的每个项目,您可以像这样提取 JSONObject 和属性:
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
jsonObject.getString("text");
}
就实际存储数据而言,如上所述,JSON 数据可以有多种格式,具体取决于来源,因此,它通常在客户端解析并保存在您的应用程序类对象中以供使用。或者更一般地说,您可以使用Map<String, Object>
这很容易是我见过的最佳答案:
https://dzone.com/articles/which-is-the-right-java-abstraction-for-json
总结:共有三种抽象:pojo、地图和列表,以及表示对象、数组和原语的自定义类。每个人都有优点和缺点,没有明显的赢家。
Pojos 有最大的优势,但你不能总是使用它们。如果可以,请使用它们,如果必须,请使用其他。
如果您正在做的不是最简单的映射,那么您应该使用完整的类结构。创建您的类层次结构作为 JSON 中数据结构的镜像,并使用Jackson使用 ObjectMapper 将 JSON 直接映射到类层次结构。
使用此解决方案,您无需将 Object 转换为 Map 或弄乱 JSONObject 或 JSONArray,并且您的代码中没有任何多级映射遍历。您只需获取 JSON 字符串,将其提供给 ObjectMapper,然后获取您的 Object,其中包含由 ObjectMapper 自动映射的子对象(甚至是集合)。
XStream xstream = new XStream(new JsonHierarchicalStreamDriver());
xstream.setMode(XStream.NO_REFERENCES);
xstream.alias("myAlias", MyClass.class); // requires a no argument constructor
System.out.println(xstream.toXML(product));
好的,评论里的先生想要一个反序列化的例子,给你:
XStream xstream = new XStream(new JsonHierarchicalStreamDriver());
xstream.alias("myAlias", MyClass.class);
Product product = (Product)xstream.fromXML(json);
System.out.println(product.getName());
如果您需要进一步的帮助,请告诉我...