有没有办法将包含json的String转换为HashMap,其中每个键都是json-key,值是json-key的值?json没有嵌套值.我正在使用Gson lib.
例如,给定JSON:
{
"id":3,
"location":"NewYork"
}
Run Code Online (Sandbox Code Playgroud)
生成HashMap:
<"id", "3">
<"location", "NewYork">
Run Code Online (Sandbox Code Playgroud)
谢谢
我有一个Java类,用户:
public class User
{
int id;
String name;
Timestamp updateDate;
}
Run Code Online (Sandbox Code Playgroud)
我收到一个包含来自Web服务的用户对象的JSON列表:
[{"id":1,"name":"Jonas","update_date":"1300962900226"},
{"id":5,"name":"Test","date_date":"1304782298024"}]
Run Code Online (Sandbox Code Playgroud)
我试图编写一个自定义反序列化器:
@Override
public User deserialize(JsonElement json, Type type,
JsonDeserializationContext context) throws JsonParseException {
return new User(
json.getAsJsonPrimitive().getAsInt(),
json.getAsString(),
json.getAsInt(),
(Timestamp)context.deserialize(json.getAsJsonPrimitive(),
Timestamp.class));
}
Run Code Online (Sandbox Code Playgroud)
但我的解串器不起作用.如何为Gson编写自定义JSON反序列化器?
我目前正在开发一个Android项目,需要我调用一个Web服务,它将返回一个json文件.我一直在使用GSON库来解析所有json文件并获取一个JSON对象.它一直运行良好,直到我遇到这个关键字段是动态的json数据.此文件的示例如下:
{ "0": { "count":"5"},
"1": { "title":"...", "desc":"" },
"2": { "title":"...", "desc":"" },
"3": { "title":"...", "desc":"" },
"4": { "title":"...", "desc":"" },
"5": { "title":"...", "desc":"" },
"routes": { "route1":"...", "route3":"" },
}
Run Code Online (Sandbox Code Playgroud)
我能够根据密钥ID"0"得到计数,但我不知道如何利用这个值来获取其他密钥对象(密钥id 1-5),其中包含数据我需要.如果有人能够帮助我解决这个问题,我们将非常感激.谢谢.
让我们假设我有一个类型的Java类:
public class MyClass
{
public String par1;
public Object par2;
}
Run Code Online (Sandbox Code Playgroud)
然后我有这个:
String json = "{"par1":"val1","par2":{"subpar1":"subval1"}}";
Gson gson = new GsonBuilder.create();
MyClass mClass = gson.fromJson(json, MyClass.class);
Run Code Online (Sandbox Code Playgroud)
该par2JSON是从其他应用程序给我,我永远不知道什么是它的参数名称,因为它们是动态的.
我的问题是,par2MyClass上应该设置哪个Class类型变量,以便将JSON String变量正确反序列化为我的类对象?
谢谢
我有以下内容:
public class ChargeRequest {
@Expose
private String customerName;
@Expose
private String stripeToken;
@Expose
private String plan;
@Expose
private String[] products;
gettersAndSetters()...
public Map<String, Object> toMap() {
return gson.fromJson(this, new TypeToken<Map<String, Object>>() {
}.getType());
}
public String toString() {
return gson.toJson(this, getClass());
}
}
Run Code Online (Sandbox Code Playgroud)
我正试图转换ChargeRequest成一个Map<String, Object>与Gson.
我的适配器:
public static class JsonAdapter implements JsonDeserializer<ChargeRequest>{
@Override
public ChargeRequest deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
ChargeRequest cr = new ChargeRequest();
JsonObject o = json.getAsJsonObject();
o.add("customerName", o.get("customerName")); …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下格式创建 JSON 对象。
{
"tableID": 1,
"price": 53,
"payment": "cash",
"quantity": 3,
"products": [
{
"ID": 1,
"quantity": 1
},
{
"ID": 3,
"quantity": 2
}
]
Run Code Online (Sandbox Code Playgroud)
}
我知道如何使用 JSONObject 和 JSONArray 静态地执行此操作。但我需要一种更动态的方式,因为必须实现产品数组,因此它有很多对象而不仅仅是 2 个。
有没有办法删除 JSONObject 的内容?例如我有以下 JSONobject
{ “ID”: 3, “数量”: 2 }
我可以以某种方式删除它的值,以便我可以在迭代中重用该对象吗?
在我的 Android 应用程序(用 Kotlin 编写)中,我需要将一些 JSON 转换为 MainObject 哈希映射的字符串。JSON 如下所示:
\n\n{\n "a": {\n "name": "A",\n "some_int": "2",\n "some_string": "string",\n "some_bool": false,\n "some_string_arr": [\n "str1",\n "str2"\n ],\n "sub_obj_arr": [\n {\n "obj_name": "d",\n "some_obj_string": "s"\n }\n ]\n },\n "b": {\n "name": "B",\n "some_int": "4",\n "some_string": "string",\n "some_bool": false,\n "some_string_arr": [\n "str5",\n "str6"\n ]\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我创建了几个对象来帮助解决这个问题。
\n\nclass MainObject {\n @SerializedName("name")\n val name: String? = null\n\n @SerializedName("some_int")\n val someInt: Int? = null\n\n @SerializedName("some_string")\n val someString: String? = null\n\n @SerializedName("some_bool")\n val someBool: …Run Code Online (Sandbox Code Playgroud) 有没有办法将不同的String-Collections合并为一个JSON'String'?我想要一个看起来像这样的JSON字符串:
{"vendor":[Sun, HP, IBM],"product":[bla, bla, bla],"availability":[high, mid, low],"capacity":[bla, bla, bla], ...}
Run Code Online (Sandbox Code Playgroud)
这是我的Java代码的一部分:
Collection<String> vendor = bh.getAllVendors();
Collection<String> product = bh.getProductsForVendor(vendor);
Collection<String> availability = bh.getAllAvailabilities();
Collection<String> capacity = bh.getCapacityForVendor(vendor);
Collection<String> signaling = bh.getSignalingForVendor(vendor);
Collection<String> backup = bh.getBackupForVendor(vendor);
Gson gson = new Gson();
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激.