我有一些像这样的输入值:
customername
phone
email
.....
some items values
itemname1
itemname2
........
在每个项目名称中都有一些单位价格 数量 ........
我试图像这样在json中编码这些值:
{
"info": {
"customername": "abc",
"phone": "123",
"email": "an@gmail.com",
},
"item": {
"itemname1": {
"unitprice": "100",
"qty": "3",
},
"itemname2": {
"unitprice": "500",
"qty": "2",
}
}
}
我无法对上述所有值进行编码。
这是我的代码:
private void jsonEncoding() throws JSONException {
JSONObject obj = new JSONObject();
JSONObject obj1 = new JSONObject();
JSONObject obj2 = new JSONObject();
try {
obj1.put("name", name);
obj1.put("email", email);
obj1.put("phone", phone);
} catch (JSONException e) {
e.printStackTrace();
}
obj.put("info",obj1 );
System.out.print(obj);
System.out.print(obj1);
}
如何编码上述json格式的值。
答案 0 :(得分:3)
您可以通过两种方式实现:
使用Gson/Jackson
等第三方库将POJO类数据转换为json object string
(使用http://www.jsonschema2pojo.org/轻松创建POJO类)
手动执行JsonParsing,如下所示:
try { // info node JSONObject objInfo = new JSONObject(); objInfo.put("name", name); objInfo.put("email", email); objInfo.put("phone", phone); // itemname1 node JSONObject itemname1 = new JSONObject(); itemname1.put("unitprice", unitprice1); itemname1.put("qty", qty1); // itemname2 node JSONObject itemname2 = new JSONObject(); itemname2.put("unitprice", unitprice2); itemname2.put("qty", qty2); // item node JSONObject item = new JSONObject(); // adding itemname1 & itemname2 to item item.put("itemname1",itemname1); item.put("itemname2",itemname2); // root node JSONObject root = new JSONObject(); root.put("info",objInfo); root.put("item",item); System.out.print(root); } catch(JsonException e){ e.printStackTrace(); }
答案 1 :(得分:1)
试试这可能会有所帮助
您需要创建三个类,因为您使用Gson意味着您将使用网络功能,因此可以实现以下所有类型化。
class CustomObject implements Serializable {
Info info;
Map<String, ItemName> item;
public CustomObject() {
this(null, null);
}
public CustomObject(Info info, Map<String, ItemName> item) {
this.info = info;
this.item = item;
}
public Info getInfo() {
return info;
}
public void setInfo(Info info) {
this.info = info;
}
public Map<String, ItemName> getItem() {
return item;
}
public void setItem(Map<String, ItemName> item) {
this.item = item;
}
}
class Info implements Serializable {
String customername;
String phone;
String email;
public Info() {
this("","","");
}
public Info(String customername, String phone, String email) {
this.customername = customername;
this.phone = phone;
this.email = email;
}
public String getCustomername() {
return customername;
}
public void setCustomername(String customername) {
this.customername = customername;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
class ItemName implements Serializable {
String unitprice;
String qty;
public ItemName() {
this("", "");
}
public ItemName(String unitprice, String qty) {
this.unitprice = unitprice;
this.qty = qty;
}
public String getUnitprice() {
return unitprice;
}
public void setUnitprice(String unitprice) {
this.unitprice = unitprice;
}
public String getQty() {
return qty;
}
public void setQty(String qty) {
this.qty = qty;
}
}
现在您必须根据需要添加数据,如下所示。
CustomObject customObject = new CustomObject();
Info info = new Info();
info.setCustomername("abc");
info.setPhone("123");
info.setEmail("an@gmail.com");
customObject.setInfo(info);
ItemName itemname1 = new ItemName();
itemname1.setUnitprice("100");
itemname1.setQty("3");
ItemName itemname2 = new ItemName();
itemname2.setUnitprice("500");
itemname2.setQty("2");
Map<String, ItemName> item = new HashMap<>();
item.put("itemname1", itemname1);
item.put("itemname2", itemname2);
customObject.setItem(item);
System.out.println(new Gson().toJson(customObject));
以上System.out.println将输出显示为
{
"info":
{
"customername":"abc",
"phone":"123",
"email":"an@gmail.com"},
"item":{
"itemname1":
{
"unitprice":"100",
"qty":"3"
},
"itemname2":
{
"unitprice":"500",
"qty":"2"
}
}
}
现在如何解析从服务器或任何源到该CustomObject类的字符串。通过谷歌通过Gson库非常简单。
String str = "{\n" +
" \"info\":\n" +
" {\n" +
" \"customername\":\"abc\",\n" +
" \"phone\":\"123\",\n" +
" \"email\":\"an@gmail.com\"\n" +
" },\n" +
" \"item\": \n" +
" {\n" +
" \"itemname1\":\n" +
" {\n" +
" \"unitprice\":\"100\",\n" +
" \"qty\":\"3\"\n" +
" },\n" +
" \"itemname2\":\n" +
" {\n" +
" \"unitprice\":\"500\",\n" +
" \"qty\":\"2\"\n" +
" }\n" +
" }\n" +
" }";
CustomObject customObject = new Gson().fromJson(str, new TypeToken<CustomObject>(){}.getType());
System.out.println(new Gson().toJson(customObject));