0

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","john@asd.com"));
users.add(new UserProfile("cena",brock","brockceno@asd.com"));

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
}
the bosxcz
  • 91
  • 3
  • 13
  • Oh my, to a Java object, this one is going to be hard ! – adeneo Feb 01 '14 at 02: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 01 '14 at 02:41
  • I'm on my bloody phone so can't flag properly, but possible duplicate of: http://stackoverflow.com/questions/1395551/convert-a-json-string-to-object-in-java – Paul Richter Feb 01 '14 at 02: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 01 '14 at 02:42
  • @FelixKling it is the other way around i want to parse JSON from javascript to java object. – the bosxcz Feb 01 '14 at 02:44
  • So you want to parse a javascript object to Java so you can set the value of an input on the serverside ? – adeneo Feb 01 '14 at 02:45
  • @adeneo i have edited my question i indicated there the steps. – the bosxcz Feb 01 '14 at 02:56
  • I'm still right after the edit: You *receive* JSON from the sever as response and you have to parse it to a JavaScript array or object. Either you don't know that the `success: function()` part is JavaScript, or you simply got the terminology wrong. – Felix Kling Feb 01 '14 at 03:02
  • possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Felix Kling Feb 01 '14 at 03:04
  • success:function(data) is where i get the response from the servlet using ajax and from there i want to iterate the elements of the json string to set it for example to a table i want to display john garcia and cena brock to a table. – the bosxcz Feb 01 '14 at 03:12

1 Answers1

0

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>
btafarelo
  • 601
  • 3
  • 12