如何以我的 json 格式 post 数据
How to post data in my json format
我是 android 的新手,使用 volley 库混淆了 json 格式的 post 数据。
我的 Json 参数如:
{
key
comKey
addLeadArray
[{
key
autoid
images[image1,image2...]
audio
}
{
key
autoid
images[image1,image2...]
audio
}
{
key
autoid
images[image1,image2...]
audio
}.....]
}
我正在尝试代码:
JSONObject object = new JSONObject();
object.put("key", "1");
object.put("comKey", "2");
................
JSONObject addLeadArrayObj= new JSONObject();
AddLeadArray.put("key", "1");
AddLeadArray.put("autoid", "34");
................
object.put("addLeadArray", addLeadArrayObj);
但是它创建了 {{}},我想为上面的 json 甲酸创建 json 对象。我能做什么请帮助我,请提供代码片段。
如何使用 Gson.
Gson gson = new Gson(); // Or use new GsonBuilder().create();
MyType target = new MyType();
String json = gson.toJson(target); // serializes target to Json
MyType target2 = gson.fromJson(json, MyType.class); // deserializes json into target2
如果您 serializing/deserializing 的对象是 ParameterizedType(即至少包含一个类型参数并且可能是一个数组),那么您必须使用 toJson(Object, Type)
或 fromJson(String, Type)
方法。
这是一个序列化和反序列化 ParameterizedType 的例子:
Type listType = new TypeToken<List<String>>() {}.getType();
List<String> target = new LinkedList<String>();
target.add("blah");
Gson gson = new Gson();
String json = gson.toJson(target, listType);
List<String> target2 = gson.fromJson(json, listType);
这里是 Gson Help.
您正在做的是创建 JSONObject AddLeadArray 并将其放入主 JsonObject 中。
AddLeadArray 是一个数组,因此将其作为 JSONArray 的对象而不是 JSONObject。
JSONObject object = new JSONObject();
object.put("key", "1");
object.put("comKey", "2");
................
// Create JSONArray
JSONArray addLeadArrayObj= new JSONArray();
// Create JSONObject for jsonArray
JSONObject object2 = new JSONObject();
object2.put("key", "1");
object2.put("autoid", "34");
// put object2 in addLeadArray
addLeadArrayObj.put(object2);
// put addLeadArray in main jsonobject
object.put("addLeadArray", addLeadArrayObj);
在此处参考更多内容:Add JsonArray to JsonObject
我是 android 的新手,使用 volley 库混淆了 json 格式的 post 数据。 我的 Json 参数如:
{
key
comKey
addLeadArray
[{
key
autoid
images[image1,image2...]
audio
}
{
key
autoid
images[image1,image2...]
audio
}
{
key
autoid
images[image1,image2...]
audio
}.....]
}
我正在尝试代码:
JSONObject object = new JSONObject();
object.put("key", "1");
object.put("comKey", "2");
................
JSONObject addLeadArrayObj= new JSONObject();
AddLeadArray.put("key", "1");
AddLeadArray.put("autoid", "34");
................
object.put("addLeadArray", addLeadArrayObj);
但是它创建了 {{}},我想为上面的 json 甲酸创建 json 对象。我能做什么请帮助我,请提供代码片段。
如何使用 Gson.
Gson gson = new Gson(); // Or use new GsonBuilder().create();
MyType target = new MyType();
String json = gson.toJson(target); // serializes target to Json
MyType target2 = gson.fromJson(json, MyType.class); // deserializes json into target2
如果您 serializing/deserializing 的对象是 ParameterizedType(即至少包含一个类型参数并且可能是一个数组),那么您必须使用 toJson(Object, Type)
或 fromJson(String, Type)
方法。
这是一个序列化和反序列化 ParameterizedType 的例子:
Type listType = new TypeToken<List<String>>() {}.getType();
List<String> target = new LinkedList<String>();
target.add("blah");
Gson gson = new Gson();
String json = gson.toJson(target, listType);
List<String> target2 = gson.fromJson(json, listType);
这里是 Gson Help.
您正在做的是创建 JSONObject AddLeadArray 并将其放入主 JsonObject 中。
AddLeadArray 是一个数组,因此将其作为 JSONArray 的对象而不是 JSONObject。
JSONObject object = new JSONObject();
object.put("key", "1");
object.put("comKey", "2");
................
// Create JSONArray
JSONArray addLeadArrayObj= new JSONArray();
// Create JSONObject for jsonArray
JSONObject object2 = new JSONObject();
object2.put("key", "1");
object2.put("autoid", "34");
// put object2 in addLeadArray
addLeadArrayObj.put(object2);
// put addLeadArray in main jsonobject
object.put("addLeadArray", addLeadArrayObj);
在此处参考更多内容:Add JsonArray to JsonObject