如何正确地将对象放入JSONObject?

时间:2021-04-30 19:37:48

I'm trying to fill my JSONObject like this:

我正在尝试像这样填充我的JSONObject:

JSONObject json = new JSONObject();
json.put("Command", "CreateNewUser");
json.put("User", user);

user is instance of basic class that contains fields like "FirstName", "LastName" etc.

user是基本类的实例,包含“FirstName”,“LastName”等字段。

Looks like I'm doing it wrong, because I get JSON like this:

看起来我做错了,因为我得到这样的JSON:

{
    "Command":"CreateNewUser",
    "User":"my.package.name.classes.User@2686a150"
}

instead of "tree".

而不是“树”。

What is wrong with my code?

我的代码出了什么问题?

5 个解决方案

#1


20  

Since you use JSONObject to represent non-primitive types, any instance passed to JSONObject.put(Object, Object) will generate nested items (or trees).

由于您使用JSONObject来表示非基本类型,因此传递给JSONObject.put(Object,Object)的任何实例都将生成嵌套项(或树)。

JSONObject main = new JSONObject();
main.put("Command", "CreateNewUser");
JSONObject user = new JSONObject();
user.put("FirstName", "John");
user.put("LastName", "Reese");
main.put("User", user);
{
    "User": {
        "FirstName": "John",
        "LastName": "Reese"
    },
    "Command": "CreateNewUser"
}

#2


3  

The framework you are using does not know how to convert your User object to a Map, that is used internally as JSON representation and so it is using standard 'toString' method that you have not overriden.

您正在使用的框架不知道如何将您的User对象转换为Map,它在内部用作JSON表示,因此它使用您没有覆盖的标准'toString'方法。

Just export all properties (for example write method 'Map toMap()' on your User type ) of your User to a Map (all values must be standard JDK types) and put that map in your json object:

只需将用户的所有属性(例如,用户类型的写入方法“Map toMap()”)导出到Map(所有值必须是标准JDK类型)并将该映射放在json对象中:

json.put("User", user.toMap())

It will do the thing.

它会做的事情。

#3


2  

This will do what you want !

这将做你想要的!

JSONObject json = new JSONObject();
json.put("Command", "CreateNewUser");
json.put("User", new JSONObject(user));

#4


0  

From http://developer.android.com/reference/org/json/JSONObject.html#put

 public JSONObject put (String name, Object value)

Parameters value a JSONObject, JSONArray, String, Boolean, Integer, Long, Double, NULL, or null. May not be NaNs or infinities.

参数值为JSONObject,JSONArray,String,Boolean,Integer,Long,Double,NULL或null。可能不是NaN或无穷大。

Though your user is subclass of Object, it is not the type that the put method expects.

虽然您的用户是Object的子类,但它不是put方法所期望的类型。

The android SDK's implementation of JSONObject seems lacking a put(java.lang.String key, java.util.Map value) method (from the same link above). You may need to add a method toMap() in your user class to convert it into a HashMap. Then finally use json.put("user", new JSONObject(user.toMap()));

android SDK的JSONObject实现似乎缺少put(java.lang.String key,java.util.Map value)方法(来自上面的相同链接)。您可能需要在用户类中添加一个方法toMap()以将其转换为HashMap。然后最后使用json.put(“user”,new JSONObject(user.toMap()));

#5


0  

You can solve it using fasterxml ObjectMapper.

您可以使用fasterxml ObjectMapper解决它。

ObjectMapper o = new ObjectMapper();
String userJsonString = o.readValueAsString(user);

JSONObject jsonObj = new JSONObject();
jsonObj.put("Command", "CreateNewUser");
jsonObj.put("User", new JSONObject(userJsonString));

You will get following output:

您将获得以下输出:

{
    "User": {
        "FirstName": "John",
        "LastName": "Reese"
    },
    "Command": "CreateNewUser"
}

Ref: https://github.com/FasterXML/jackson-databind/

#1


20  

Since you use JSONObject to represent non-primitive types, any instance passed to JSONObject.put(Object, Object) will generate nested items (or trees).

由于您使用JSONObject来表示非基本类型,因此传递给JSONObject.put(Object,Object)的任何实例都将生成嵌套项(或树)。

JSONObject main = new JSONObject();
main.put("Command", "CreateNewUser");
JSONObject user = new JSONObject();
user.put("FirstName", "John");
user.put("LastName", "Reese");
main.put("User", user);
{
    "User": {
        "FirstName": "John",
        "LastName": "Reese"
    },
    "Command": "CreateNewUser"
}

#2


3  

The framework you are using does not know how to convert your User object to a Map, that is used internally as JSON representation and so it is using standard 'toString' method that you have not overriden.

您正在使用的框架不知道如何将您的User对象转换为Map,它在内部用作JSON表示,因此它使用您没有覆盖的标准'toString'方法。

Just export all properties (for example write method 'Map toMap()' on your User type ) of your User to a Map (all values must be standard JDK types) and put that map in your json object:

只需将用户的所有属性(例如,用户类型的写入方法“Map toMap()”)导出到Map(所有值必须是标准JDK类型)并将该映射放在json对象中:

json.put("User", user.toMap())

It will do the thing.

它会做的事情。

#3


2  

This will do what you want !

这将做你想要的!

JSONObject json = new JSONObject();
json.put("Command", "CreateNewUser");
json.put("User", new JSONObject(user));

#4


0  

From http://developer.android.com/reference/org/json/JSONObject.html#put

 public JSONObject put (String name, Object value)

Parameters value a JSONObject, JSONArray, String, Boolean, Integer, Long, Double, NULL, or null. May not be NaNs or infinities.

参数值为JSONObject,JSONArray,String,Boolean,Integer,Long,Double,NULL或null。可能不是NaN或无穷大。

Though your user is subclass of Object, it is not the type that the put method expects.

虽然您的用户是Object的子类,但它不是put方法所期望的类型。

The android SDK's implementation of JSONObject seems lacking a put(java.lang.String key, java.util.Map value) method (from the same link above). You may need to add a method toMap() in your user class to convert it into a HashMap. Then finally use json.put("user", new JSONObject(user.toMap()));

android SDK的JSONObject实现似乎缺少put(java.lang.String key,java.util.Map value)方法(来自上面的相同链接)。您可能需要在用户类中添加一个方法toMap()以将其转换为HashMap。然后最后使用json.put(“user”,new JSONObject(user.toMap()));

#5


0  

You can solve it using fasterxml ObjectMapper.

您可以使用fasterxml ObjectMapper解决它。

ObjectMapper o = new ObjectMapper();
String userJsonString = o.readValueAsString(user);

JSONObject jsonObj = new JSONObject();
jsonObj.put("Command", "CreateNewUser");
jsonObj.put("User", new JSONObject(userJsonString));

You will get following output:

您将获得以下输出:

{
    "User": {
        "FirstName": "John",
        "LastName": "Reese"
    },
    "Command": "CreateNewUser"
}

Ref: https://github.com/FasterXML/jackson-databind/