Przekazuję tablicę json z działania A do czynności B. Następnie używam biblioteki GSON do wstawienia wartości do tablicy. To jest mój obecny kod.Konwertowanie tablicy json na obiekt json w systemie Android przy użyciu gson?
public void gsonResponse(String json) {
try {
JSONObject jsonObject = new JSONObject(json);
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", textViewInvNo.getText().toString());
linkedHashMap.put("itembarcode", innerJosonObject.getString("itembarcode"));
linkedHashMap.put("net_wt", innerJosonObject.getString("net_wt"));
linkedHashMap.put("gross_wt", innerJosonObject.getString("gross_wt"));
linkedHashMap.put("stone_wt", innerJosonObject.getString("stone_wt"));
linkedHashMap.put("stone_amt", innerJosonObject.getString("stone_amt"));
linkedHashMap.put("rate", innerJosonObject.getString("rate"));
linkedHashMap.put("making", innerJosonObject.getString("making"));
linkedHashMap.put("qty", innerJosonObject.getString("qty"));
linkedHashMap.put("net_rate", innerJosonObject.getString("net_rate"));
linkedHashMap.put("item_total", innerJosonObject.getString("item_total"));
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);
String jsonResp = jsonObject.toString();
jsonFormattedString = jsonResp.replaceAll("\\\\","");
Log.d("NEW JSON", jsonFormattedString);
} catch (JSONException e) {
e.printStackTrace();
}
}
Wyjście na to: -
{"result":["{"doc_no":"ES101","itembarcode":"BRMS","net_wt":"10","gross_wt":"1","stone_wt":"0","stone_amt":"0","rate":"32000","making":"100","qty":"1","net_rate":"32100.0","item_total":"32100.0","sum_total":"64600.0"}",
"{"doc_no":"ES101","itembarcode":"MSAA0015","net_wt":"10","gross_wt":"11","stone_wt":"100000","stone_amt":"1","rate":"32000","making":"500","qty":"1","net_rate":"32500.0","item_total":"32500.0","sum_total":"64600.0"}"]}
Ale moja pożądane wyjście powinno być coś takiego: -
[{"doc_no":"IN1001","itembarcode":"BRMS123456\nFLT22K","net_wt":"10","gross_wt":"10","stone_amt":"0","rate":"29000","making":"999","qty":"1","net_rate":"29999.0","item_total":"29999.0","sum_total":"30299.0","stone_wt":"0"},
{"doc_no":"IN1001","itembarcode":"BRMS\nGA24K","net_wt":"10","gross_wt":"1","stone_amt":"0","rate":"32000","making":"100","qty":"1","net_rate":"","item_total":"","sum_total":"30299.0","stone_wt":""}]
Jak mogę to osiągnąć? Każda sugestia lub pomoc jest doceniana. Dziękuję.
Hej to jest proste i wydaje się działać Jedynym problemem jest to, że dostać dodatkowe quotes.How mogę pozbądź się ich. ["{" doc_no ":" IN1002 "," kod pozycji ":" BRMS "," net_wt ":" 10 "," gross_wt ":" 1 "," stone_wt ":" 0 "," stone_amt ":" 0 " "rate": "32000", "making": "100", "qty": "1", "net_rate": "32100.0", "item_total": "32100.0", "sum_total": "64600.0"} " , "{" doc_no ":" IN1002 "," kod pozycji ":" MSAA0015 "," net_wt ":" 10 "," gross_wt ":" 11 "," stone_wt ":" 100000 "," stone_amt ":" 1 "," rate ":" 32000 "," making ":" 500 "," qty ":" 1 "," net_rate ":" 32500.0 "," item_total ":" 32500.0 "," sum_total ":" 64600.0 "} "] –
, ale rozmiar tablicy ciągle się zmienia. Nie będzie to zawsze 2. –
daje to wyjście ["" doc_no ":" IN1002 "," kod pozycji ":" BRMS "," net_wt ":" 10 "," gross_wt ":" 1 "," stone_wt ":" 0 "," stone_amt " : "0", "rate": "32000", "making": "100", "qty": "1", "net_rate": "32100.0", "item_total": "32100.0", "sum_total": " 64600.0 "", "" doc_no ":" IN1002 "," itembarcode ":" MSAA0015 "," net_wt ":" 10 "," gross_wt ":" 11 "," stone_wt ":" 100000 "," stone_amt ":" 1 "," rate ":" 32000 "," making ":" 500 "," qty ":" 1 "," net_rate ":" 32500.0 "," item_total ":" 32500.0 "," sum_total ":" 64600.0 " "] –