java 动态创建 JSON 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45043352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create JSON object dynamically
提问by Giannis
I am trying to create JSON object with the following format.
我正在尝试使用以下格式创建 JSON 对象。
{
"tableID": 1,
"price": 53,
"payment": "cash",
"quantity": 3,
"products": [
{
"ID": 1,
"quantity": 1
},
{
"ID": 3,
"quantity": 2
}
]
}
}
I know how to do this statically using JSONObject and JSONArray. But I need a more dynamic way cause the products array must be implemented so it has many objects not just 2.
IS there a way to delete the contents of a JSONObject? eg I have the following JSONobject
{ "ID": 3, "quantity": 2 }
我知道如何使用 JSONObject 和 JSONArray 静态地做到这一点。但我需要一种更动态的方式,因为必须实现 products 数组,因此它有很多对象,而不仅仅是 2。
有没有办法删除 JSONObject 的内容?例如我有以下 JSONobject
{“ID”:3,“数量”:2}
Could I somehow erase its values so i can reuse the object in an iteration?
我可以以某种方式擦除它的值,以便我可以在迭代中重用该对象吗?
采纳答案by pashute
Try constructing your JSON data as a string:
尝试将您的 JSON 数据构建为字符串:
String json = "{" +
"\"tableID\": 1," +
"\"price\": 53," +
"\"payment\": \"cash\"," +
"\"quantity\": 3," +
"\"products\": [";
for (int i = 0; i < 100; i++) {
json += "{ \"ID\": 3, \"quantity\": 2 }";
if(i != 100) json += ",";
}
json += "]}";
and then create your JSONObject:
然后创建您的 JSONObject:
JSONObject jsonObject = new JSONObject(json);
JSONObject jsonObject = new JSONObject(json);
I do not know what you exactly want to do, but I am guessing it is something like below:
我不知道你到底想做什么,但我猜它是这样的:
for(int i = 0; i < products.size(); i++) {
json += "{" +
"\"ID\": " + products.get(i).getId() + "," +
"\"quantity\": " + products.get(i).getQuantity() + " }";
if(i != products.size() - 1) json += ",";
}
Note: See kws's answerfor a more readable way to do it.
注意:有关更易读的方法,请参阅 kws 的答案。
回答by kws
To your first question, you can build the above json dynamically like this.
对于你的第一个问题,你可以像这样动态构建上面的json。
JSONObject jsonObject = new JSONObject();
jsonObject.put("tableID", 1);
jsonObject.put("price", 53);
jsonObject.put("payment", "cash");
jsonObject.put("quantity", 3);
JSONArray products = new JSONArray();
//product1
JSONObject product1 = new JSONObject();
product1.put("ID", 1);
product1.put("quantity", 1);
products.put(product1); //add to products
//product3
JSONObject product3 = new JSONObject();
product3.put("ID", 3);
product3.put("quantity", 2);
products.put(product3); //add to products
jsonObject.put("products", products); //add products array to the top-level json object
To the second question, you can remove an element of a JSONObject if you know its name.
对于第二个问题,如果知道名称,则可以删除 JSONObject 的元素。
jsonObject.remove("tableID"); // remove the tableID key/value pair
Or if you want to remove a specific element of a JSONArray then you have to know its position in the collection
或者,如果要删除 JSONArray 的特定元素,则必须知道它在集合中的位置
jsonObject.getJSONArray("products").remove(1); //removes the second item in the collection which is the product3
回答by pashute
Woops, I wrote a whole long answer in js instead of java.
糟糕,我用 js 而不是 java 写了一个很长的答案。
Simply use a HashMap and the GSON library from Google.
只需使用 HashMap 和 Google 的 GSON 库即可。
See here: How can I convert JSON to a HashMap using Gson?
请参阅此处:如何使用 Gson 将 JSON 转换为 HashMap?
You create your object dynamically as a hashMap, adding products and removing them at will. Then you turn the object into a JSON string with toJson() method.
您可以将对象动态创建为 hashMap,随意添加和删除产品。然后使用 toJson() 方法将对象转换为 JSON 字符串。
(or start from any JSON string read into a HashMap with fromJson() and then manipulate it at will and return it to a string.
(或从使用 fromJson() 读入 HashMap 的任何 JSON 字符串开始,然后随意操作它并将其返回为字符串。
回答by VIX
Yes, being an object you can change its content. Answering your question, it would be so.
是的,作为一个对象,您可以更改其内容。回答你的问题,应该是这样。
JSONArray jsArray = (JSONArray) json.get("products");
JSONObject js1 = jsArray.getJSONObject(0);
System.out.println("0: "+js1);
jsArray.remove(0);
JSONObject js2 = jsArray.getJSONObject(0);
System.out.println("1: "+js2);
jsArray.remove(0);
Therefore, you can iterate that array or make it into a list, according to your preference
因此,您可以根据自己的喜好迭代该数组或将其放入列表中
回答by Mohamed Hussein
For the first question
对于第一个问题
You can use this websiteto create the java classes and it will create 2, one for the attributes (tableId...
) and another one for the products and simply contain array of Products in the main class
您可以使用此网站创建 java 类,它将创建 2 个,一个用于属性 ( tableId...
),另一个用于产品,并且在主类中仅包含产品数组
For the second question
对于第二个问题
you can just ignore it products[0]
and ignore products[1](Id:3)
你可以忽略它products[0]
并忽略它products[1](Id:3)