简体   繁体   English

如何在Java中检索数组值?

[英]How to retrieve Array values in Java?

In my Java Application i'm getting Request parameter like Array 在我的Java应用程序中,我得到了Array之类的Request参数

<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.. 现在,我在java中获取此请求参数并正确检索值。

//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"] 但是在Java方面,我正在获取值names=[["1000"]] ,在for循环中我正在获取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 . 如何在我的String ["1000"]删除此多余的括号,这是原始值,但我只需要1000

Please help me is their any wrong declaration in Array. 请帮助我,他们在Array中的任何错误声明。

use org.json 使用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++){

}

Just i'm using String concept to solve this Problem but this is not real solution.. 只是我正在使用String概念来解决此问题,但这不是真正的解决方案。

//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... 如果有人知道真正的解决方法,请告诉我...

instead of JSON.stringify(names) we can use names.join() this will be return Array, 可以使用names.join()而不是JSON.stringify(names)来返回Array,

so we can easily send request attribute like string @RequestParam("names") String names 这样我们就可以轻松发送请求属性,例如字符串@RequestParam("names") String names

and now using for loop we easily get values like.. 现在使用for循环,我们很容易获得像这样的值。

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

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. 我想告诉您的第一件事是生成列表或Map Structure之类的JSON,以便您可以使用任何JSON或GSON Api获得Structure。

It will make simple for you to retrieve them & no other Constraints needed in future to retrieve them. 这将使您轻松地检索它们,而以后无需其他约束来检索它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM