Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

can a javascript json string be converted to a java object in java web? 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?

ie how can i use the name of the json string to set a input text box field.

[{"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

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

the UserProfile class is consist of name last name and email.

users.add(new UserProfile("john","garcia","[email protected]"));
users.add(new UserProfile("cena",brock","[email protected]"));

and i return it to the jsp

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

and when i receive in ajax

succes:function(data){
 //i want to access every element of the arraylist that is parse to json string
}
share|improve this question
    
Oh my, to a Java object, this one is going to be hard ! –  adeneo Feb 1 '14 at 2:39
    
If I understand you correctly, then you want set the value with JavaScript, so you would have parse the JSON into a JavaScript object. And yes, either of those is possible, search for "JavaScript parse JSON" or "Java parse JSON". –  Felix Kling Feb 1 '14 at 2:41
    
I'm on my bloody phone so can't flag properly, but possible duplicate of: stackoverflow.com/questions/1395551/… –  Paul Richter Feb 1 '14 at 2:41
    
There are any number of ways to do what it appears you're trying to do. In any case, if you have a JSON string, this is trivial. Search a bit and ask a specific question if you're having a specific problem. –  Dave Newton Feb 1 '14 at 2:42
    
@FelixKling it is the other way around i want to parse JSON from javascript to java object. –  the bosxcz Feb 1 '14 at 2:44

1 Answer 1

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.

This is not java's object, just json iteration.

<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>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.