我正在尝试构建一个如下所示的JSON字符串:
{
"account": {
"description": "desc2",
"users": [
{
"user": {
"username": "Zogbi",
"password": "password1",
"firstName": "Tim",
"lastName": "Smith",
}
}
]
}
}
这是什么,但是:
{
"account": {
"users":[{
"middleName":"S","lastName":"Smith","username":"Zogbi","alias":"Gibbus","firstName":"Tim","password":"password1"
}
]
}
}
所以,有两个问题: 我在“帐户”之后需要一个“描述” 2.我需要一个“用户”对象,它是“用户”数组的一部分。
这是我的代码:
JSONObject account = new JSONObject();
JSONArray users = new JSONArray();
JSONObject user = new JSONObject();
JSONObject mainObj = new JSONObject();
account.put("users", users);
users.put("user");
user.put("username", "Zogbi");
user.put("password", "password1");
user.put("firstName", "Tim");
user.put("lastName", "Smith");
user.put("middleName", "S");
user.put("alias", "Gibbus");
mainObj.put("account", account);
感谢任何帮助!
答案 0 :(得分:1)
在将对象放入数组时删除引号。
你需要另一个对象,因为users
是一个对象数组(让我们调用userassoc),而那些对象有一个成员“user”,它是一个用户对象。像这样:
JSONObject account = new JSONObject();
JSONArray users = new JSONArray();
JSONObject userassoc = new JSONObject();
JSONObject user = new JSONObject();
JSONObject mainObj = new JSONObject();
account.put("users", users);
users.put(userassoc);
userassoc.put("user", user);
user.put("username", "Zogbi");
user.put("password", "password1");
user.put("firstName", "Tim");
user.put("lastName", "Smith");
user.put("middleName", "S");
user.put("alias", "Gibbus");
mainObj.put("account", account);
看起来像一个奇怪的JSON结构。你得到了正确的任务吗?
答案 1 :(得分:0)
您不会通过写出单个字节来编写JPEG文件,而不是手动编写单个对象和数组条目。
使用支持良好的全功能JSON序列化器/解串器框架,我最喜欢的是Jackson。
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.vertigrated.FormattedRuntimeException;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class Q26110375
{
public static void main(final String[] args)
{
final User user = new User("Zogbi", "password1", "Tim", "S", "Smith", "Gibbus");
final Account account = new Account("desc2", user);
try
{
final ObjectMapper m = new ObjectMapper();
m.enable(SerializationFeature.INDENT_OUTPUT);
m.enable(SerializationFeature.WRAP_ROOT_VALUE);
m.writeValue(System.out, account);
}
catch (final IOException e)
{
throw new FormattedRuntimeException(e, "Could not convert %s to Json because of %s", account.getClass(), e.getMessage());
}
}
private static class User
{
@JsonProperty
private final String username;
@JsonProperty
private final String password;
@JsonProperty
private final String firstName;
@JsonProperty
private final String middleName;
@JsonProperty
private final String lastName;
@JsonProperty
private final String alias;
private User(@Nonnull final String username, @Nonnull final String password, @Nonnull final String firstName,
@Nonnull final String middleName, @Nonnull final String lastName, @Nonnull final String alias)
{
this.username = username;
this.password = password;
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.alias = alias;
}
}
@JsonRootName("account")
private static class Account
{
@JsonProperty
private final String description;
@JsonProperty
private final List<User> users;
private Account(@Nonnull final String description, @Nonnull final User... users)
{
this.description = description;
this.users = Arrays.asList(users);
}
}
}
{
"account" : {
"description" : "desc2",
"users" : [ {
"username" : "Zogbi",
"password" : "password1",
"firstName" : "Tim",
"middleName" : "S",
"lastName" : "Smith",
"alias" : "Gibbus"
} ]
}
}