2

What I am trying to do is add a Map into an ArrayList and then into a JsonArray. What I intend to do is directly add the maps into the json array.

//Initialize the ArrayList,Map and Json array
private ArrayList<Map<String, String>> itemData = new ArrayList<>();
private Map<String, String> itemselected = new HashMap<>();
JSONArray itemSelectedJson = new JSONArray();



 private void selectedItems() {
    if(invEstSwitch.isChecked())
    {
        billType = textViewEstimate.getText().toString();
    }else{
        billType = textViewInvoice.getText().toString();
    }

    itemselected.put("custInfo",custSelected.toString());
    itemselected.put("invoiceNo", textViewInvNo.getText().toString());
    itemselected.put("barcode", barCode.getText().toString());
    itemselected.put("desc", itemDesc.getText().toString());
    itemselected.put("weight", weightLine.getText().toString());
    itemselected.put("rate", rateAmount.getText().toString());
    itemselected.put("makingAmt", makingAmount.getText().toString());
    itemselected.put("net_rate", netRate.getText().toString());
    itemselected.put("itemTotal", itemtotal.getText().toString());
    itemselected.put("vat", textViewVat.getText().toString());
    itemselected.put("sum_total", textViewSum.getText().toString());
    itemselected.put("bill_type", billType);
    itemselected.put("date", textViewCurrentDate.getText().toString());

    //Add the map to the Array
    itemData.add(index, itemselected);
    itemSelectedJson= new JSONArray(Arrays.asList(itemData));
    index++;
}
Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73
  • What are you trying to do here?why are you doing this? – AndroidNewBee Nov 27 '15 at 11:15
  • What is the issue you are getting – Pankaj Nov 27 '15 at 11:15
  • @AndroidNewBee I want it to be a jsonArray so that I can parse it and using volley insert this data into the mysql database.Please Help I am new to programming:) Thanks –  Nov 27 '15 at 11:17
  • What json you want to make?? can you give example – Pankaj Nov 27 '15 at 11:19
  • @Clairvoyant I would like to insert the maps directly into the jsonarray.But since I did not know how to do that I 1st put the map data into the arraylist and then converted it into a json array.Is there a way in which I can do the same thing using json array.Thanks –  Nov 27 '15 at 11:20
  • Maybe this helps: http://stackoverflow.com/questions/12155800/how-to-convert-hashmap-to-json-object-in-java – user2390182 Nov 27 '15 at 11:21
  • Thanks for the edits @AnoopM –  Nov 27 '15 at 11:29

2 Answers2

2

You can do this way:

JSONArray jRootArray = new JSONArray();

        for (int i = 1; i <= 20; i++) {
            JSONObject jInnerObject = new JSONObject();
            try {
                jInnerObject.put(String.valueOf(i), "Hello "+i);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            jRootArray.put(jInnerObject);
        }

        Log.i("JRootArray", jRootArray.toString());

Hope this will help you.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
  • can you elaborate I would like to try this. –  Nov 27 '15 at 11:49
  • @user3295583, you have to put your key and value in your json object and after filling data you have put json object into json array. – Hiren Patel Nov 27 '15 at 12:00
  • @HirenPatel please check the below code how can i use your code in this scenario.Even I am trying to do something very similar.Thanks – AndroidNewBee Nov 27 '15 at 12:59
1

Try this:

private Map<String, String> itemselected = new HashMap<>();   

private void selectedItems() {

    JSONArray itemSelectedJson = new JSONArray();

    // Retrieve all keys
    Set<String> keys = itemselected.keySet(); 

    // Add items to JSONArray as JSONObjects
    for(String key : keys) {
        itemSelectedJson.put(
            new JSONObject().put(key, itemselected.get(key))
        );
    }
}

This way, you won't have to pass by an ArrayList to fill your JSONArray. Then, you can get your JSON string simply by calling the toString() method on the JSON array:

String jsonString = itemSelectedJson.toString();
Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
  • can you please elaborate i would like to try this code.Thanks –  Nov 27 '15 at 11:28
  • Yes I would be using the volley library for that. –  Nov 27 '15 at 11:30
  • The thing is, I've never used Volley or even Retrofit before, so you need to tell me exactly what you have to do to send a successful request to Volley. – Mohammed Aouf Zouag Nov 27 '15 at 11:31
  • I am new to programming so I am trying to follow this blog. https://www.simplifiedcoding.net/android-volley-post-request-tutorial/ –  Nov 27 '15 at 11:34
  • I see that there is a `getParams()` method that is already defined to append parameters to your **Volley request**, where they return a **Map**. I think you should do the same. – Mohammed Aouf Zouag Nov 27 '15 at 11:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/96348/discussion-between-user3295583-and-jdev). –  Nov 27 '15 at 11:44