0

I have this array list in java

[ {"pname":"7", "qty":"222"}, 
  {"pname":"8", "qty":"5"}, 
  {"pname":"9", "qty":"60"} ]

I can access the first index which is object, how can I access the first element inside the first object which is "pname" key in java syntax. Please give me sample codes. Thanks.

I tried:

mylist.get(0)

but it only gives me the first object. I don't know how to access the first index inside the object. here is my whole code from getting the data to parse it into json array and convert to array list

        String data = request.getParameter("data");
        JSONArray jsonArray = new JSONArray(data);

        ArrayList<String> mylist = new ArrayList<String>();
        JSONArray this_is_jsonArray = (JSONArray)jsonArray; 
        if (jsonArray == null) { 
            System.out.println("json is empty");
        }
        else
        {
           int length = this_is_jsonArray.length();
           for (int i=0;i<length;i++){ 
               mylist.add(this_is_jsonArray.get(i).toString());
           }    
        }
        output.append(mylist);

Basically I'm trying to do a function similar output to this mylist[0].pname in javascript. the expected output all in all is to save those pnames and qtys to a variable for me to able to send each value to the database

Marvs.M
  • 13
  • 7
  • what is your idea of _first index inside the first object_? Do you mean the `"pname"` key? – Nick Parsons Aug 07 '19 at 07:14
  • yes mate, exactly, sorry for not including that – Marvs.M Aug 07 '19 at 07:15
  • Inside `myList`, what is the datatype you are using? Is it String or Some Object? – ersnh Aug 07 '19 at 07:41
  • ArrayList mylist = new ArrayList(); – Marvs.M Aug 07 '19 at 07:49
  • [{"pname":"7","qty":"222"}, {"pname":"8","qty":"5"}, {"pname":"9","qty":"60"}] this is exactly the calue of mylist – Marvs.M Aug 07 '19 at 07:50
  • As far as I understand, you have an array of `String`s containing JSON. You then need to parse these strings using any JSON parser, this would allow you to access the structure. See e.g. https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java – Forketyfork Aug 07 '19 at 08:00
  • What you paste here is either a String or an ArrayList. Can you please specify? In case it is an ArrayList which is the type of the contained objects? HashMap? – Pitto Aug 07 '19 at 08:02
  • 1
    please see the post above, I edited it and included my code, thanks – Marvs.M Aug 07 '19 at 08:17
  • You're converting a `JSONObject` to string in this line: `mylist.add(this_is_jsonArray.get(i).toString())` - don't convert it, because you lose the structure. Do `this_is_jsonArray.getJSONObject(i).getString("pname")` instead. – Forketyfork Aug 07 '19 at 08:32
  • If i don't convert it it won't add, how can I add it? Please help – Marvs.M Aug 07 '19 at 08:42

2 Answers2

0

In order to write a proper answer you need to be very clear about the input and output you have and you expect.

I don't understand why you want to create a parallel data structure instead of using the parsed JSON but from what I read in comments I think that you need to change the structure of your ArrayList content in order to obtain the result you want to achieve.

String data = "[ {\"pname\":\"7\", \"qty\":\"222\"}, {\"pname\":\"8\", \"qty\":\"5\"}, {\"pname\":\"9\", \"qty\":\"60\"} ]" ;
HashMap<String, String> item = new HashMap<String, String>();
JSONArray jsonArray = new JSONArray(data);
ArrayList<HashMap> mylist = new ArrayList<HashMap>();
if (jsonArray == null) {
    System.out.println("json is empty");
} else {
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject = jsonArray.getJSONObject(i);
        item.put("pname", jsonObject.getString("pname"));
        item.put("qty", jsonObject.getString("qty"));
        mylist.add(item);
    }
}
System.out.println(mylist);
Pitto
  • 8,229
  • 3
  • 42
  • 51
  • length() I converted this to size() cause it's giving me error but getJSONObject() function gives me error too, – Marvs.M Aug 07 '19 at 08:26
  • @Marvs.M Please provide details on the expected output you want. I wrote a new version based on your comments above. – Pitto Aug 07 '19 at 08:51
  • for example I need to put each object value to a variable so I can use them to add those values to the data base like this: {"pname":"7", "qty":"222"} like I'm accessing this first index in array and I want to put the pname value to one variable then put qty value to another variable – Marvs.M Aug 07 '19 at 09:01
  • hmap("pname", this_is_jsonArray.get(i).getString("pname")); have error on getString() function here. The ide only suggest getClass() – Marvs.M Aug 07 '19 at 09:05
  • Corrected @Marvs.M. Please try again. You still need to specify / confirm that the expected output is what I built in the code. – Pitto Aug 07 '19 at 09:18
  • 1
    yes @Pitto the expected out put is what you built to your code – Marvs.M Aug 07 '19 at 09:27
  • not really, this syntax "hmap("pname", currentItem.getString("pname"));" does not really accept the getString() function, it's an error to my ide. – Marvs.M Aug 07 '19 at 10:46
  • @Marvs.M Sorry but I have no access to a pc at the moment. Can you try last update? – Pitto Aug 07 '19 at 12:10
  • I'll try it when I get access to my pc later. Thanks for your help mate. I really appreciate it – Marvs.M Aug 08 '19 at 01:37
  • This line "JSONObject currentItem = this_is_jsonArray.get(i);" give this error "Object cannot be converted to JSONObjec" when I do this "JSONObject currentItem = (JSONObject) this_is_jsonArray.get(i);" And this line "hmap("pname", currentItem.get("pname"));" "cannot find this method hmap(String, Object)" – Marvs.M Aug 08 '19 at 05:30
  • @Marvs.M Sorry I finally had access to a PC. This is a fully tested example. – Pitto Aug 08 '19 at 09:08
0

First thing to consider is JSON object is not ordered.The first object can be pname or qty, in successive request. To access the fields, give field name as an associative array.

JSONArray jsonArray = new JSONArray(data);

    ArrayList<String> mylist = new ArrayList<String>();
    JSONArray this_is_jsonArray = (JSONArray)jsonArray; 
    if (jsonArray == null) { 
        System.out.println("json is empty");
    }
    else
    {
       int length = this_is_jsonArray.length();
       for (int i=0;i<length;i++){ 

     // Just this line is modified 
     mylist.add(this_is_jsonArray.getJSONObject(i).getString("pname").toString());

       }    
    }
jay
  • 65
  • 1
  • 10