0

In my Java Application i'm getting Request parameter like Array

<script type="text/javascript">
   var names = new Array();
    $.ajax({
        url : "Result",
        type : 'POST',
        data : {
            "names" : JSON.stringify(names),
            "globalClassId" : globalClassId
        }});
</script>

Now i'm getting this request parameter in java and retrieve the values correctly..

//Get the each added user using names[]
    for (String id : names) {
        -----
                    -----
    }

But in java side i'm getting values names=[["1000"]] and in for loop i'm getting id=["value1"]

if we pass this value to database for example select query,But it is giving null value.. even we pass correct value but we are getting null value.

How to remove this extra brackets in my String ["1000"] this is original value but i want only 1000.

Please help me is their any wrong declaration in Array.

1
  • 1
    Use a JSON library to parse your JSON input. You have GSon, Jackson and the common library from json.org Commented Jul 15, 2013 at 4:22

4 Answers 4

1

use org.json

 JSONObject obj = new JSONObject(request.getParameter("names"));


List<String> list = new ArrayList<String>();
JSONArray array = obj.getJSONArray("interests");
for(int i = 0 ; i < array.length() ; i++){

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

Comments

1

Just i'm using String concept to solve this Problem but this is not real solution..

//Get the each added user using names[]
    for (String id : names) {
        userVO = userService.getUser(id.substring(2, id.length()-2));
        userList.add(userVO);
    }

If any body know real solution Please tell me...

Comments

1

instead of JSON.stringify(names) we can use names.join() this will be return Array,

so we can easily send request attribute like string @RequestParam("names") String names

and now using for loop we easily get values like..

//Get the each added user using names[]
    for (String id : names.split(",")) {
        ----------
    }

Comments

0

First thing i want to tell you that generate your JSON like a list or Map Structure so that you can get Structure using any JSON or GSON Api.

It will make simple for you to retrieve them & no other Constraints needed in future to retrieve them.

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.