我是JSON的新手,所以请原谅我的无知。我需要像这样获得一个JSON对象:
jsonString =
{
"Key": {"AppID":"19","Username":"sompoRoot","Password":"11223344"},
"deviceList": ["43ab48a0eb9f950d9c2498f8c2cfa1e5b8a62687479cfad849bbc455b88e67b6",
"43ab48a0eb9f950d9c2498f8c2cfa1e5b8a62687479cfad849bbc455b88e67b6"],
"aps": {"alert":"merhaba dunya","sound":"default","badge":"10",
"dictionaryArray":["a","b"],
"production":"false"}
}我会从数据库中获取AppID,UserName和密码。类似于那个:43ab48a0eb9f950d9c2498f8c2cfa1e5b8a62687479cfad849bbc455b88e67b6,43ab48a0eb9f950d9c2498f8c2cfa1e5b8a62687479cfad849bbc455b88e67b6...的是deviceList我将从文本输入获得警报,声音,徽章dictionaryArray和生产。我得到并转换字符串的这些参数。我在网上搜索,但我认为它更复杂,我怎么才能得到那样的JSON,谢谢。
发布于 2013-09-06 19:44:23
您在评论中说,每个字段都有一个单独的字符串。我建议使用以下方式构建一个包含所有所需字段的class (由于aps似乎是一个单独的实体,请为它创建一个单独的类):
public class MyClass {
private String key;
private List<String> deviceList;
private Aps aps;
}
public class Aps {
private String alert;
private String sound;
private long badge;
private List<String> dictionaryArray;
private boolean production;
}有几个库可以帮助您将Java对象转换为JSON,Jackson's ObjectMapper class就是其中之一。您可以下载from their website库,或者如果您在项目中使用Maven,您可以找到对mvnrepository.com的依赖。
使用ObjectMapper
ObjectMapper objectMapper = new ObjectMapper();
MyClass myClass = new MyClass();
// .. populate the fields
String jsonString = objectMapper.writeValueAsString(myClass);您必须将其包含在try-catch块中。仅此而已:jsonString现在保存转换为JSON字符串的字段。
编辑:而不是杰克逊你可以使用Google GSON,它的工作方式大致相同。
发布于 2013-09-06 20:18:33
package com.stackoverflow.q1688099;
import java.util.List;
import com.google.gson.Gson;
public class Test {
public static void main(String... args) throws Exception {
String json =
"{"
+ "'title': 'Computing and Information systems',"
+ "'id' : 1,"
+ "'children' : 'true',"
+ "'groups' : [{"
+ "'title' : 'Level one CIS',"
+ "'id' : 2,"
+ "'children' : 'true',"
+ "'groups' : [{"
+ "'title' : 'Intro To Computing and Internet',"
+ "'id' : 3,"
+ "'children': 'false',"
+ "'groups':[]"
+ "}]"
+ "}]"
+ "}";
// Now do the magic.
Data data = new Gson().fromJson(json, Data.class);
// Show it.
System.out.println(data);
}
}
class Data {
private String title;
private Long id;
private Boolean children;
private List<Data> groups;
public String getTitle() { return title; }
public Long getId() { return id; }
public Boolean getChildren() { return children; }
public List<Data> getGroups() { return groups; }
public void setTitle(String title) { this.title = title; }
public void setId(Long id) { this.id = id; }
public void setChildren(Boolean children) { this.children = children; }
public void setGroups(List<Data> groups) { this.groups = groups; }
public String toString() {
return String.format("title:%s,id:%d,children:%s,groups:%s", title, id, children, groups);
}
}查看此link
发布于 2013-09-06 19:33:51
你可以使用java的JSON库(GSON,Jettison等)。之后,将数据添加到Map对象(嵌套结构)中,并使用库中的实用程序方法将其转换为JSON。
使用Jettison查找示例
JSONObject json = new JSONObject();
JSONObject jsonKey = new JSONObject();
jsonKey.put("AppID", 19);
jsonKey.put("Username", "usrname");
jsonKey.put("Password", "passwd");
String[] deviceList = {"value1", "value2"};
json.put("key", jsonKey);
json.put("deviceList", deviceList);
String finalJson = json.toString();https://stackoverflow.com/questions/18656697
复制相似问题