我使用intent extra将一个json数组作为字符串从一个活动到另一个活动。如何在java中的json数组中的某个位置插入一个值。 这是我正在使用的json数组。
{"result":[{"itembarcode":"BRMS","weight":"10","gross_wt":"1","stone_amt":"0","stone_wt":"","rate":"32000","making":"100","qty":"1","net_rate":"32100.0","item_total":"32100.0","sum_total":"64600.0"},
{"itembarcode":"MSAA0015","weight":"10","gross_wt":"11","stone_amt":"100000","stone_wt":"","rate":"32000","making":"500","qty":"1","net_rate":"32500.0","item_total":"32500.0","sum_total":"64600.0"}]}
我想在这个数组中插入一个类似
的doc_no {"result":[{"doc_no":"IN1001","itembarcode":"BRMS","weight":"10","gross_wt":"1","stone_amt":"0","stone_wt":"","rate":"32000","making":"100","qty":"1","net_rate":"32100.0","item_total":"32100.0","sum_total":"64600.0"},
{"doc_no":"IN1001","itembarcode":"MSAA0015","weight":"10","gross_wt":"11","stone_amt":"100000","stone_wt":"","rate":"32000","making":"500","qty":"1","net_rate":"32500.0","item_total":"32500.0","sum_total":"64600.0"}]}
我尝试过这样的事情
try {
JSONArray jr = new JSONArray(json);
for (int j=0;j<tot_length; j++)
{
JSONObject jb = jr.getJSONObject(j);
String docnumber = "IN1001";
jb.put("doc_no",docnumber);
}
Log.d("NEW JSON",json);
} catch (JSONException e) {
e.printStackTrace();
}
但它对我不起作用。 任何帮助或建议表示赞赏。谢谢。
答案 0 :(得分:1)
JSONObject不支持管理排序..所以你需要使用像GSON
这样的库我使用GSON做了这个..
试试
try {
JSONObject jsonObject = new JSONObject(yourJsonString);
JSONArray jsonArray = jsonObject.getJSONArray("result");
for(int i=0;i<jsonArray.length();i++){
LinkedHashMap<String,String> linkedHashMap = new LinkedHashMap<>();
JSONObject innerJosonObject = new JSONObject(jsonArray.getString(i));
// you need to put all values from jsonObject to map for managing the order..
linkedHashMap.put("doc_no","Custom Value");
linkedHashMap.put("itembarcode",innerJosonObject.getString("itembarcode"));
linkedHashMap.put("weight",innerJosonObject.getString("weight"));
linkedHashMap.put("gross_wt", innerJosonObject.getString("gross_wt"));
//..................... rest of others......
linkedHashMap.put("sum_total",innerJosonObject.getString("sum_total"));
Gson gson = new Gson();
// convert linkedHashMap to json string and it will keep the insertion order..
String string = gson.toJson(linkedHashMap,LinkedHashMap.class);
jsonArray.put(i,string);
}
jsonObject.put("result",jsonArray);
Log.e("json",jsonObject.toString());
// this prints jsonArray only [............]
Log.e("json_array", jsonArray.toString());
}catch (Exception e){
}
Output:`{"result":["{\"doc_no\":\"Custom Value\",\"itembarcode\":\"BRMS\",\"weight\":\"10\",\"gross_wt\":\"1\",\"sum_total\":\"64600.0\"}",
"{\"doc_no\":\"Custom Value\",\"itembarcode\":\"MSAA0015\",\"weight\":\"10\",\"gross_wt\":\"11\",\"sum_total\":\"64600.0\"}"]}`
将此添加到gradle文件
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.code.gson:gson:2.3.1'
compile 'com.android.support:appcompat-v7:22.2.1'
}
希望它有所帮助..谢谢
答案 1 :(得分:0)
您需要在数组中再次存储已更改的jsonObject 试试这段代码
try {
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject1 = new JSONObject(
"{ \r\n \"itembarcode\":\"BRMS\",\r\n \"weight\":\"10\",\r\n \"gross_wt\":\"1\",\r\n \"stone_amt\":\"0\",\r\n \"s\u200C\u200Btone_wt\":\"\",\r\n \"rate\":\"32000\",\r\n \"making\":\"100\",\r\n \"qty\":\"1\",\r\n \"net_rate\":\"32100.0\",\r\n \"item_to\u200C\u200Btal\":\"32100.0\",\r\n \"sum_total\":\"64600.0\"\r\n }");
JSONObject jsonObject2 = new JSONObject(
"{ \r\n \"itembarcode\":\"MSAA0015\",\r\n \"weight\":\"10\",\r\n \"gross_wt\":\"11\",\r\n \"stone_amt\":\"100000\",\r\n \"st\u200C\u200Bone_wt\":\"\",\r\n \"rate\":\"32000\",\r\n \"making\":\"500\",\r\n \"qty\":\"1\",\r\n \"net_rate\":\"32500.0\",\r\n \"item_tot\u200C\u200Bal\":\"32500.0\",\r\n \"sum_total\":\"64600.0\"\r\n }");
jsonArray.put(jsonObject1);
jsonArray.put(jsonObject2);
JSONObject finalObject = new JSONObject();
finalObject.put("result", jsonArray);
System.out.println("Old----->" + finalObject);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject json = jsonArray.getJSONObject(i);
json.put("doc_no", "IN1001");
jsonArray.put(i, json);
finalObject.put("result", jsonArray);
}
System.out.println("New----->" + finalObject);
} catch (Exception e) {
e.printStackTrace();
}
Old----->{"result":[{"sum_total":"64600.0","making":"100","stone_amt":"0","rate":"32000","qty":"1","weight":"10","net_rate":"32100.0","gross_wt":"1","item_to\u200c\u200btal":"32100.0","s\u200c\u200btone_wt":"","itembarcode":"BRMS"},{"sum_total":"64600.0","making":"500","stone_amt":"100000","item_tot\u200c\u200bal":"32500.0","rate":"32000","qty":"1","st\u200c\u200bone_wt":"","weight":"10","net_rate":"32500.0","gross_wt":"11","itembarcode":"MSAA0015"}]}
New----->{"result":[{"sum_total":"64600.0","making":"100","stone_amt":"0","rate":"32000","qty":"1","doc_no":"IN1001","weight":"10","net_rate":"32100.0","gross_wt":"1","item_to\u200c\u200btal":"32100.0","s\u200c\u200btone_wt":"","itembarcode":"BRMS"},{"sum_total":"64600.0","making":"500","stone_amt":"100000","item_tot\u200c\u200bal":"32500.0","rate":"32000","qty":"1","doc_no":"IN1001","st\u200c\u200bone_wt":"","weight":"10","net_rate":"32500.0","gross_wt":"11","itembarcode":"MSAA0015"}]}
编辑我没有意识到还有一个jsonObject:)