如何使用java在json对象中附加键值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32498872/
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
How to append key - values in json object using java
提问by Pankaj Kumar Katiyar
I am using org.json.JSONObject package to create a JSONArray by filling up values from db, I can get the values from db and put them a key - pair in json, but at the end of the program: key and values are not appended whereas only the last row of array is converted into json object. I want them to be appended in the final json. Any help will be appreciated.
我正在使用 org.json.JSONObject 包通过填充 db 中的值来创建一个 JSONArray,我可以从 db 中获取值并将它们放在 json 中的键对,但在程序结束时:键和值不是附加,而只有数组的最后一行被转换为 json 对象。我希望它们被附加到最终的 json 中。任何帮助将不胜感激。
try {
MobileTestClass_Methods.InitializeConfiguration();
String sqlQuery = "SELECT id, NAME FROM campaign LIMIT 4; ";
Connection con = MobileTestClass_Methods.CreateSQLConnection();
String [][] arr = MobileTestClass_Methods
.ExecuteMySQLQueryReturnsArrayWithColumnName(con, sqlQuery);
JSONObject json = new JSONObject();
int totalrow =arr.length;
int totalcolumn =arr[0].length;
System.out.println("row: "+totalrow + " col: "+totalcolumn);
for(int i=1; i<totalrow; i++) {
for(int j=0; j<totalcolumn; j++) {
String key = arr[0][j];
String value = arr[i][j];
json.put(key, value);
System.out.println("Key: "+key + " value: "+value);
}
}
System.out.println("Json: "+json);
JSONArray array = new JSONArray();
array.put(json);
JSONObject main = new JSONObject();
main.put("result", array);
System.out.println("Final Json Array: "+main);
} catch(Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
Output:
输出:
row: 5 col: 2
Key: id value: 25256
Key: NAME value: megha_video
Key: id value: 32168
Key: NAME value: Mukesh_13Aug_vpaid
Key: id value: 25258
Key: NAME value: vast
Key: id value: 32167
Key: NAME value: SDK-rtb-hudson-130815
Json: {"id":"32167","NAME":"SDK-rtb-hudson-130815"}
Final Json Array: {"result":[{"id":"32167","NAME":"SDK-rtb-hudson-130815"}]}
采纳答案by Farrandu
The result of your query returns always the keys id
and NAME
, so you're overwriting the values continuously.
您的查询结果始终返回键id
和NAME
,因此您会不断覆盖这些值。
If you want to have multiple objects with id
and NAME
properties you should use a JSONArray
and create nested JSONObject
s inside:
如果您想拥有多个具有id
和NAME
属性的对象,您应该使用 aJSONArray
并在其中创建嵌套的JSONObject
s:
// ... previous code
String [][] arr = MobileTestClass_Methods.ExecuteMySQLQueryReturnsArrayWithColumnName(con, sqlQuery);
JSONArray json = new JSONArray ();
int totalrow =arr.length;
int totalcolumn =arr[0].length;
System.out.println("row: "+totalrow + " col: "+totalcolumn);
for(int i=1; i<totalrow; i++)
{
JSONObject row = new JSONObject();
for(int j=0; j<totalcolumn; j++)
{
String key = arr[0][j];
String value = arr[i][j];
row.put(key, value);
System.out.println("Key: "+key + " value: "+value);
}
json.put(row);
}
// ... rest of the code ...
回答by Pankaj Kumar Katiyar
Well, I needed to create a json array before starting the loop and then store the json object in the first loop and finally add this json object to json array.
好吧,我需要在开始循环之前创建一个 json 数组,然后将 json 对象存储在第一个循环中,最后将此 json 对象添加到 json 数组中。
String [][] arr = MobileTestClass_Methods.ExecuteMySQLQueryReturnsArrayWithColumnName(con, sqlQuery);
JSONObject json = new JSONObject();
int totalrow =arr.length;
int totalcolumn =arr[0].length;
System.out.println("row: "+totalrow + " col: "+totalcolumn);
JSONArray array = new JSONArray();
for(int i=1; i<totalrow; i++)
{
JSONObject row = new JSONObject();
for(int j=0; j<totalcolumn; j++)
{
String key = arr[0][j];
String value = arr[i][j];
row.put(key, value);
System.out.println("Key: "+key + " value: "+value);
}
array.put(row);
}