无法构建正确的 JSON 字符串
Not able to frame correct JSON String
我正在学习 JSON 并使用 gson 构建 json 字符串。
这就是我需要的。
{
"version": "1.0",
"interface_type": "web",
"request_type": "1",
"username": "test",
"password": "test",
"sender_id": "test",
"doctor_advice": false,
"type": "test",
"bulk": "test",
"msg": "test",
"recipients": [
919845098450,
919845098451
]
}
但我越来越喜欢这个了。
{
"version": "1.0",
"interface_type": "web",
"request_type": "1",
"username": "test",
"password": "test",
"sender_id": "test",
"doctor_advice": false,
"type": "test",
"bulk": "test",
"msg": "test",
"recepients": [
"9845098450",
"9845098451",
{}
]
}
请告诉我如何提出上述要求。
这是代码
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
public class gsonWriter {
public static void main(String args[]) {
JsonObject request = new JsonObject();
request.addProperty("version", "1.0");
request.addProperty("interface_type", "web");
request.addProperty("request_type", "1");
request.addProperty("username", "test");
request.addProperty("password", "test");
request.addProperty("sender_id", "test");
request.addProperty("doctor_advice", false);
request.addProperty("type", "test");
request.addProperty("bulk", "test");
request.addProperty("msg", "test");
JsonArray recepients = new JsonArray();
JsonObject recepientList = new JsonObject();
recepients.add(new JsonPrimitive("9845098450"));
recepients.add(new JsonPrimitive("9845098451"));
recepients.add(recepientList);
request.add("recepients", recepients);
Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls()
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.create();
System.out.println(gson.toJson(request));
}
}
而且我不想用 java-pojo
来做同样的事。需要纯字符串才能通过 URL。
非常感谢您的帮助。
另外,请给我推荐一个比 gson 更好的开源 Json 活页夹,可以是 Jackson
、Json
、Genson
或任何其他可以帮助我开发的活页夹成熟的应用程序,其中应用程序不断访问 restful 服务并从相同的 :).
接收响应
在 recipients JSonArray
中添加 recipientsList
应该是没有必要的。删除这些行,您将获得与您请求的相同的输出。现在您要添加一个空 JSonObject
,由 {}
显示。
这个片段有两个不同点:
"recepients": [
"9845098450",
"9845098451",
{}
]
收件人必须是数字,不能是字符串。
不应该有那个 {}
空对象。
正确代码:
JsonObject request = new JsonObject();
request.addProperty("version", "1.0");
request.addProperty("interface_type", "web");
request.addProperty("request_type", "1");
request.addProperty("username", "test");
request.addProperty("password", "test");
request.addProperty("sender_id", "test");
request.addProperty("doctor_advice", false);
request.addProperty("type", "test");
request.addProperty("bulk", "test");
request.addProperty("msg", "test");
JsonArray recepients = new JsonArray();
recepients.add(new JsonPrimitive(9845098450L));
recepients.add(new JsonPrimitive(9845098451L));
request.add("recepients", recepients);
Gson gson = new GsonBuilder()
.setPrettyPrinting()
.serializeNulls()
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.create();
System.out.println(gson.toJson(request));
要将收件人作为数字传递,您需要在 JsonPrimitive 构造函数中传递一个数字。您的值对于 int
类型来说太大,因此我们需要通过添加后缀 L
.
使它们成为 long
为了避免空 {}
对象,只需删除 recepientList
变量,根本不需要它。
我正在学习 JSON 并使用 gson 构建 json 字符串。 这就是我需要的。
{
"version": "1.0",
"interface_type": "web",
"request_type": "1",
"username": "test",
"password": "test",
"sender_id": "test",
"doctor_advice": false,
"type": "test",
"bulk": "test",
"msg": "test",
"recipients": [
919845098450,
919845098451
]
}
但我越来越喜欢这个了。
{
"version": "1.0",
"interface_type": "web",
"request_type": "1",
"username": "test",
"password": "test",
"sender_id": "test",
"doctor_advice": false,
"type": "test",
"bulk": "test",
"msg": "test",
"recepients": [
"9845098450",
"9845098451",
{}
]
}
请告诉我如何提出上述要求。
这是代码
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
public class gsonWriter {
public static void main(String args[]) {
JsonObject request = new JsonObject();
request.addProperty("version", "1.0");
request.addProperty("interface_type", "web");
request.addProperty("request_type", "1");
request.addProperty("username", "test");
request.addProperty("password", "test");
request.addProperty("sender_id", "test");
request.addProperty("doctor_advice", false);
request.addProperty("type", "test");
request.addProperty("bulk", "test");
request.addProperty("msg", "test");
JsonArray recepients = new JsonArray();
JsonObject recepientList = new JsonObject();
recepients.add(new JsonPrimitive("9845098450"));
recepients.add(new JsonPrimitive("9845098451"));
recepients.add(recepientList);
request.add("recepients", recepients);
Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls()
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.create();
System.out.println(gson.toJson(request));
}
}
而且我不想用 java-pojo
来做同样的事。需要纯字符串才能通过 URL。
非常感谢您的帮助。
另外,请给我推荐一个比 gson 更好的开源 Json 活页夹,可以是 Jackson
、Json
、Genson
或任何其他可以帮助我开发的活页夹成熟的应用程序,其中应用程序不断访问 restful 服务并从相同的 :).
在 recipients JSonArray
中添加 recipientsList
应该是没有必要的。删除这些行,您将获得与您请求的相同的输出。现在您要添加一个空 JSonObject
,由 {}
显示。
这个片段有两个不同点:
"recepients": [
"9845098450",
"9845098451",
{}
]
收件人必须是数字,不能是字符串。
不应该有那个
{}
空对象。
正确代码:
JsonObject request = new JsonObject();
request.addProperty("version", "1.0");
request.addProperty("interface_type", "web");
request.addProperty("request_type", "1");
request.addProperty("username", "test");
request.addProperty("password", "test");
request.addProperty("sender_id", "test");
request.addProperty("doctor_advice", false);
request.addProperty("type", "test");
request.addProperty("bulk", "test");
request.addProperty("msg", "test");
JsonArray recepients = new JsonArray();
recepients.add(new JsonPrimitive(9845098450L));
recepients.add(new JsonPrimitive(9845098451L));
request.add("recepients", recepients);
Gson gson = new GsonBuilder()
.setPrettyPrinting()
.serializeNulls()
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.create();
System.out.println(gson.toJson(request));
要将收件人作为数字传递,您需要在 JsonPrimitive 构造函数中传递一个数字。您的值对于 int
类型来说太大,因此我们需要通过添加后缀 L
.
long
为了避免空 {}
对象,只需删除 recepientList
变量,根本不需要它。