java 将对象插入到 jsonarray 中的特定位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34717980/
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
Insert object to specific position in jsonarray
提问by Atul Bhardwaj
I want to add an object to a specific position in JSonArray. My Current JsonArray look like this
我想将一个对象添加到 JSonArray 中的特定位置。我当前的 JsonArray 看起来像这样
{
"imgs": [
"String1",
"String2",
"String3",
"String4"
]
}
I need to insert one more item in jsonarray at 1st position like this-
我需要在 jsonarray 中的第一个位置再插入一个项目 -
jsonArray.put(1,"String5")
this is replacing item at first position But I need below result
这是在第一个位置替换项目但我需要以下结果
{
"imgs": [
"String1",
"String5",
"String2",
"String3",
"String4"
]
}
Please suggest
请建议
采纳答案by Johannes Jander
Android JSONArray
is not meant as a general-purpose data structure, but to (de)serialize data to and from JSON. Therefore, you should do all add/remove/mutate operations on a java.util.List
or something and only if you want to send it over the wire convert to a JSONArray
.
AndroidJSONArray
并不意味着作为通用数据结构,而是将数据(反)序列化到 JSON 和从 JSON 中序列化。因此,您应该对 ajava.util.List
或其他内容执行所有添加/删除/变异操作,并且仅当您想通过网络将其发送时才将其转换为JSONArray
.
Java EE 7 also has a JsonArray
class, in their API description they make abundantly clear that they want to prevent people from using it as a replacement for a java.util.list
: JsonArray instances are list objects that provide read-only access to the values in the JSON array. Any attempt to modify the list, whether directly or using its collection views, results in an UnsupportedOperationException.
Java EE 7 也有一个JsonArray
类,在他们的 API 描述中,他们非常清楚地表明他们希望阻止人们使用它来替代一个java.util.list
:JsonArray instances are list objects that provide read-only access to the values in the JSON array. Any attempt to modify the list, whether directly or using its collection views, results in an UnsupportedOperationException.
See also this answer
另请参阅此答案
回答by hungps
Seem too old but you can do like this:
看起来太旧了,但你可以这样做:
public void addToPos(int pos, JSONObject jsonObj, JSONArray jsonArr){
for (int i = jsonArr.length(); i > pos; i--){
jsonArr.put(i, jsonArr.get(i-1));
}
jsonArr.put(pos, jsonObj);
}
回答by Eric B.
Try this:
试试这个:
String myString = jsonArray.getString(1);
jsonArray.put(1,"String5");
jsonArray.put(myString);
回答by Akash kv
call this function like this
像这样调用这个函数
String changed_json_string= changeJson(jsonString,2,"hai")
String changed_json_string= changeJson(jsonString,2,"hai")
public String changeJson(String json,int index,String add_string){
try {
JSONObject job=new JSONObject(json);
JSONArray jsonArray=job.getJSONArray("imgs");
ArrayList<String> arrayList=new ArrayList<>();
for (int i=0;i<jsonArray.length();i++){
String abc= (String) jsonArray.get(i);
arrayList.add(abc);
}
arrayList.add(index,add_string);
JSONObject a=new JSONObject();
JSONArray jar=new JSONArray();
for (String str:
arrayList) {
jar.put(str);
}
a.put("imgs",jar);
return a.toString();
} catch (JSONException e) {
e.printStackTrace();
}
return "";
}
output
输出
{"imgs":["String1","String2","hai","String3","String4"]}
回答by Krishna
Use array shifting logic or create copy of new json array with newly inserted element and insert into original json ojbect.
使用数组移位逻辑或使用新插入的元素创建新 json 数组的副本并插入到原始 json 对象中。
回答by Developer
Use a JsonArrayBuilder
object.
使用一个JsonArrayBuilder
对象。
private JsonArray insertAt(int index, String value, JsonArray origArray) {
JsonArrayBuilder resultBuilder = Json.createBuilderFactory(null).createArrayBuilder();
// Insert elements before the position
for (JsonValue elemValue : origArray.subList(0, index)) {
resultBuilder.add(elemValue);
}
// Insert your element
resultBuilder.add(value);
// Insert the rest of elements.
for (JsonValue elemValue : origArray.subList(index, origArray.size())) {
resultBuilder.add(elemValue);
}
return resultBuilder.build();
}
Note that the arguments check is not done here. Also instead of having a string as value to insert you can have a JsonValue to be more generic.
请注意,此处未进行参数检查。此外,您可以使用 JsonValue 作为更通用的值,而不是将字符串作为要插入的值。