简体   繁体   English

将javascript json字符串转换为java对象

[英]convert javascript json string to java object

can a javascript json string be converted to a java object in java web? javascript json字符串可以转换为Java Web中的Java对象吗? because i converted a arraylist to json string and send it to a jsp page, and in jsp page i want to iterate the json string, or is there any other way other than converting it to java object? 因为我将arraylist转换为json字符串并将其发送到jsp页面,并且在jsp页面中我想迭代json字符串,或者除了将其转换为java对象之外还有其他方法吗?

ie how can i use the name of the json string to set a input text box field. 即我如何使用json字符串的名称来设置输入文本框字段。

[{"name":"john"},{"lastname":"nice"}];

and in input text 并在输入文字中

$("#textbox").val("namehere");

is it possible? 可能吗?

edited: 编辑:

here are the steps that i am doing 这是我正在做的步骤

i have a button which triggers ajax and in the servlet i parse a arraylist to json 我有一个触发ajax的按钮,在servlet中,我将arraylist解析为json

ArrayList<UserProfile> users=new ArrayList<UserProfile>();

the UserProfile class is consist of name last name and email. UserProfile类由姓氏和电子邮件组成。

users.add(new UserProfile("john","garcia","john@asd.com"));
users.add(new UserProfile("cena",brock","brockceno@asd.com"));

and i return it to the jsp 然后将其返回给jsp

out.println(gson.toJson(users));

and when i receive in ajax 当我收到ajax

succes:function(data){
 //i want to access every element of the arraylist that is parse to json string
}

If i understood your ask, you can do like this code. 如果我理解您的要求,您可以喜欢此代码。

But, if your result is a array "of java objects", you cannot use a form, you need a table or list to display all data or the form data are replaced by last item of your array. 但是,如果结果是“ java对象”数组,则不能使用表单,需要一个表或列表来显示所有数据,或者表单数据将替换为数组的最后一项。

This is not java's object, just json iteration. 这不是java的对象,只是json迭代。

<form>
    <input type="text" id="name" name="name" />
    <input type="text" id="lastname" name="lastname" />
</form>

<script type="text/javascript">
    //your code

    success:function(data){
        for(i in data)
            $("#" + i).val(data[i]);
    }
</script>

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

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