java 修改/更新 jsonArray 中的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34151997/
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
Modifying/Updating values inside a jsonArray?
提问by AndroidNewBee
What I want to do is at a particular index position change/replace a value inside a json array.After going through the documentation at http://www.json.org/javadoc/org/json/JSONArray.htmlI found out that jsonArray does not have a getIndex() method.In this situation how do I update my json array at a given index position. This is the method that creates a json array in my android code.
我想要做的是在特定的索引位置更改/替换 json 数组中的值。通过http://www.json.org/javadoc/org/json/JSONArray.html 上的文档后,我发现jsonArray 没有 getIndex() 方法。在这种情况下,我如何在给定的索引位置更新我的 json 数组。这是在我的 android 代码中创建一个 json 数组的方法。
private void createJsonArray() {
billType = (invEstSwitch.isChecked() ? textViewEstimate : textViewInvoice)
.getText().toString();
String invNumber = textViewInvNo.getText().toString();
String bcode = barCode.getText().toString();
String description = itemDesc.getText().toString();
String wt = weightLine.getText().toString();
String rateAmt = rateAmount.getText().toString();
String making = makingAmount.getText().toString();
String netr = netRate.getText().toString();
String iTotal = itemtotal.getText().toString();
String vatAmt = textViewVat.getText().toString();
String sumAmt = textViewSum.getText().toString();
String crtDate = textViewCurrentDate.getText().toString();
try {
jsonObject.put("custInfo", custSelected.toString());
jsonObject.put("invoiceNo", invNumber);
jsonObject.put("barcode", bcode);
jsonObject.put("description", description);
jsonObject.put("weight", wt);
jsonObject.put("rate", rateAmt);
jsonObject.put("makingAmt", making);
jsonObject.put("net_rate", netr);
jsonObject.put("itemTotal", iTotal);
jsonObject.put("vat", vatAmt);
jsonObject.put("sum_total", sumAmt);
jsonObject.put("bill_type", billType);
jsonObject.put("date", crtDate);
} catch (JSONException e) {
e.printStackTrace();
}
try {
itemSelectedJson.put(index, jsonObject);
index++;
} catch (JSONException e) {
e.printStackTrace();
}
}
And this is the code that I use to update my json array which contains a json object.
这是我用来更新包含 json 对象的 json 数组的代码。
try {
itemSelectedJson.getJSONObject(i).put("net_rate",netChange);
Log.d("NETRATE_TW",itemSelectedJson.toString());
} catch (JSONException e) {
e.printStackTrace();
}
Now the problem with this code is it updates the jsonArray everytime a new item is added to the code.So the first object values are the same as the last object.
现在这段代码的问题是每次向代码添加新项目时它都会更新 jsonArray。所以第一个对象值与最后一个对象相同。
Also note that I am using this code inside a text watcher.So the afterTextchanged() method looks like this.
另请注意,我在文本观察器中使用此代码。因此 afterTextchanged() 方法如下所示。
@Override
public void afterTextChanged(Editable s) {
String netChange = netRate.getText().toString();
final int row_id = (int) newRow.getTag();
if ((row_id<0) || (row_id> itemSelectedJson.length())){
return;
}
try {
itemSelectedJson.getJSONObject(row_id-1).put("net_rate",netChange);
Log.d("NETRATE_TW",itemSelectedJson.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
};
This is the snapshot of what my database looks like.
这是我的数据库外观的快照。
回答by Achyuth Madhav
A jSONObject(which is a collection of name,value pairs) can be converted into a JSONArray which is an ordered array of the "values" in the JSONObject.
一个 jSONObject(它是一个名称、值对的集合)可以转换成一个 JSONArray,它是 JSONObject 中“值”的有序数组。
This can be done using the .toJSONArray()method.
这可以使用.toJSONArray()方法来完成。
When you need to replace/update the JSONArray, you may use the method .put(int index, java.util.Map value)
当您需要替换/更新 JSONArray 时,您可以使用 .put(int index, java.util.Map value) 方法
Unlike how you are doing at present, i.e getting the object and setting a new key and value.
与您目前的做法不同,即获取对象并设置新的键和值。
http://www.json.org/javadoc/org/json/JSONArray.html#put(int, java.util.Map)
http://www.json.org/javadoc/org/json/JSONArray.html#put(int, java.util.Map)
回答by Muhammed Refaat
As I understood, your problem is in creating multiple JSONObjects
in your JSONArray
with the same values, and that's because you can't get the index
of a created JSONObject
inside your JSONArray
, and to overcome this problem, you can easily create a compound JSONObject
that contains your JSONObjects
instead of a JSONArray
contains JSONObjects
, and the object name will be anything unique in your JSONObject
data, and when you make any changes or add any item it will either be added as a new object if it doesn't exist or it will overwrite the existing object if it added before, for example let's suppose that barcodevalue is unique in your data, so the code will be like the following:
我的理解,你的问题是创建多个JSONObjects
在你JSONArray
拥有相同的值,这是因为你不能得到index
的创建JSONObject
你的内部JSONArray
,并克服这个问题,你可以轻松地创建一种化合物JSONObject
,它包含你的JSONObjects
,而不是JSONArray
contains JSONObjects
,并且对象名称将是您JSONObject
数据中唯一的任何内容,并且当您进行任何更改或添加任何项目时,如果它不存在,它将作为新对象添加,或者如果它之前添加,它将覆盖现有对象,例如让我们假设条码值在您的数据中是唯一的,因此代码将如下所示:
// declaring itemSelectedJson as JSONObject
JSONObject itemSelectedJson = new JSONObject();
.
.
.
// when wanting to add a new item
itemSelectedJson.put(jsonObject.getString("barcode"), jsonObject);
and to retrieve the data you simple iterate through this JSONObject
:
并检索您通过此简单迭代的数据JSONObject
:
Iterator<?> keys = itemSelectedJson.keys();
JSONObject single_item;
while(keys.hasNext()) {
String key = (String)keys.next();
single_item = itemSelectedJson.getJSONObject(key);
// do what do you want here
}