伙计我正在使用jqgrid ..这是我的文件
{
"rows":[
{"OrderID":"10248","FromDate":"1996-07-04","CustomerID":"WILMK","ShipName":"Vins et alcools Chevalier","ToDate":"1996-07-05"},
{"OrderID":"10249","FromDate":"1996-07-05","CustomerID":"TRADH","ShipName":"Toms Spezialit\u00e4ten","ToDate":"1996-07-17"},
{"OrderID":"10250","FromDate":"1996-07-08","CustomerID":"HANAR","ShipName":"Hanari Carnes","ToDate":"1996-07-26"},
{"OrderID":"10251","FromDate":"1996-07-08","CustomerID":"VICTE","ShipName":"Victuailles en stock","ToDate":"1996-08-01"},
{"OrderID":"10277","FromDate":"1996-08-09","CustomerID":"MORGK","ShipName":"Morgenstern Gesundkost","ToDate":"1996-08-12"}
]
}

但我希望它像
{
"rows":[
{"OrderID":"10248","FromDate":"1996-07-04","CustomerID":"WILMK","ShipName":"Vins et alcools Chevalier","ToDate":"1996-07-05"},
{"OrderID":"10249","FromDate":"1996-07-05","CustomerID":"TRADH","ShipName":"Toms Spezialit\u00e4ten","ToDate":"1996-07-17"},
{"OrderID":"10250","FromDate":"1996-07-08","CustomerID":"HANAR","ShipName":"Hanari Carnes","ToDate":"1996-07-26"},
{"OrderID":"10251","FromDate":"1996-07-08","CustomerID":"VICTE","ShipName":"Victuailles en stock","ToDate":"1996-08-01"},
{"OrderID":"10277","FromDate":"1996-08-09","CustomerID":"MORGK","ShipName":"Morgenstern Gesundkost","ToDate":"1996-08-12"},
{"OrderID":"10261","FromDate":"1996-07-19","CustomerID":"QUEDE","ShipName":"Que Del\u00edcia","ToDate":"1996-08-09"} // added row
]
}

任何帮助plzz .. thansk提前
答案 0 :(得分:0)
从你的问题看来,你想在数组中的json文件中添加另一个对象。所以,这是一个解决方案。
答案 1 :(得分:0)
首先解析您的JSON内容:
String input = "{\"rows\":[{\"OrderID\":\"10248\"}]}";
JSONObject json = new JSONObject(input);
然后从解析的内容中获取数组:
JSONArray arr = json.getJSONArray("rows");
将对象转换为添加到JSON:
JSONObject newRow = new JSONObject();
newRow.accumulate("OrderID", 10000);
// Do the same for all your attributes
最后将新行添加到数组中以获取更新的JSON:
arr.put(newRow);
String updatedJSON = json.toString();
答案 2 :(得分:0)
您想在数组中的json
文件中添加另一个对象。所以,这是一个解决方案。
阅读json
文件。
将您的密钥row
作为JSONArray
并将其存储到字符串中。
构建一个班级Order
。
Class Order
{
String OrderID ;
String FromDate ;
String CustomerID
String ShipName;
String ToDate;
}
在特定列表中添加新对象。
//import Class (add a jar com.fasterxml.jackson to your project)
import com.fasterxml.jackson.databind.ObjectMapper;
// Here I assume that "applicationArray" contains the Array .
String applicationArray = "[
{"OrderID":"10248","FromDate":"1996-07-04","CustomerID":"WILMK","ShipName":"Vins et alcools Chevalier","ToDate":"1996-07-05"},
{"OrderID":"10249","FromDate":"1996-07-05","CustomerID":"TRADH","ShipName":"Toms Spezialit\u00e4ten","ToDate":"1996-07-17"},
{"OrderID":"10250","FromDate":"1996-07-08","CustomerID":"HANAR","ShipName":"Hanari Carnes","ToDate":"1996-07-26"},
{"OrderID":"10251","FromDate":"1996-07-08","CustomerID":"VICTE","ShipName":"Victuailles en stock","ToDate":"1996-08-01"},
{"OrderID":"10277","FromDate":"1996-08-09","CustomerID":"MORGK","ShipName":"Morgenstern Gesundkost","ToDate":"1996-08-12"},
{"OrderID":"10261","FromDate":"1996-07-19","CustomerID":"QUEDE","ShipName":"Que Del\u00edcia","ToDate":"1996-08-09"} // added row
]"
ObjectMapper mapper = new ObjectMapper();
List<Order> obj_listOrder = mapper.readValue(applicationArray, mapper.getTypeFactory().constructCollectionType(List.class, DeviceApplication.class));
现在再向列表中添加一个元素。
向List中添加一个元素。 替换您的文件的内容。