1

My javascript code. I am creating a jsonarray with some elements. Now i want to retrieve that data retrieve and want to store in a java json object for further process.

var priceList = []; priceList.push({"itemId":itemId1,"idElem":idElem1,"isActive":isActive1,"description":description1}); priceList.push({"itemId":itemId2,"idElem":idElem2,"isActive":isActive2,"description":description2}); var param = {'objarray' : priceList, 'user' : user};

    $.ajax({
      url : 'cPayment,
      type: 'POST', 
      dataType: 'json',  
      data: param,
      success: function(result) {
          alert('SUCCESS');
      }
    });

In java code :

String objArray = request.getParameter("objarray");
JSONArray jsonArray = new JSONArray();
for(//for items in objArray) {
JSONObject jsonObj = new JSONObject(); 
jsonObj.put("itemId", itemId);
jsonObj.put("idElem", idElem);
jsonObj.put("isActive", isActive);
jsonObj.put("description", "description");
jsonArray.put( jsonObj);
}

Please help me in retrieving javascript json array data in java as java jsonobject.

2
  • 2
    You need to post more of your server-side code and be a little more specific as to what the actual problem is. Commented May 20, 2018 at 9:48
  • I am facing problem while converting javascript json array to java specific object and to iterate that in for loop. Commented May 20, 2018 at 10:06

3 Answers 3

1

In Ajax call try JSON.parse(param);

Sign up to request clarification or add additional context in comments.

2 Comments

I am asking about how to retrieve at server side.
$.ajax({ dataType: 'json', data: "objarray="+param, success: function(result) { } }); If needed we have to use JSON.stringify(param);
0

Try following code in java:

JSONObject jsonObject = null;
try {
        jsonObject = new JSONObject(req.getParameter("objarray"));
    } catch(JSONException _instance) {
        // Exception Handle Message
    }
    System.out.println("Entered in to the controller ");
    String itemId ="" ;
    if(jsonObject.has("itemId")) {
       name = objarray.getString("itemId");
    }

1 Comment

After catch block.What you are doing with jsonObject and where did you get objarray. I think you are missing something to add.
0

I have used the following code it worked for me.

In Javascript code:

var param = {'objarray' : JSON.stringify(priceList), 'user' : user};

And on servlet side i used org.json.simple.JSONArray and org.json.simple.JSONObject to retrive my data.

My Java code :

JSONParser parser=new JSONParser();
    try {
        String objarray = URLDecoder.decode(request.getParameter("objarray"), "UTF-8");
        jsonArrayObj = (JSONArray) parser.parse(objarray);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }

    for(int i= 0; i <jsonArrayObj.size() ;i++) {
        JSONObject psItem = (JSONObject)jsonArrayObj.get(i);
        Integer itemId = Integer.valueOf((String) psItem.get("itemId "));
        String idElem = (String) psItem.get("idElem");
        String description= (String) psItem.get("description");
        String isActive= Boolean.valueOf((String)psItem.get("isActive"));
        Syste.out.println("Item Id --> " +itemId);
        Syste.out.println("Id Element --> "+idElem );
        Syste.out.println("Description --> "+description);
        Syste.out.println("Is Active --> "+isActive);
       }

It Worked for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.