Solved

How to iterate the below JSON string and populate the values into the below text fields

Posted on 2011-09-29
13
304 Views
Last Modified: 2012-05-12
I have text fileds like below . i want to iterate the above JSON string and populate the values into the below text fields.

do i need to construct below JSON string in another format so that it is easy to populate in text fields.

[{"LastName1":"l1","FirstName1":"f1","LastName2":"l2","FirstName2":"f2"}]


<input type='text' id='lastName1'> 
<input type='text' id='firstName1'> 

<input type='text' id='lastName2'> 
<input type='text' id='firstName2'> 

<input type='text' id='lastName3'> 
<input type='text' id='firstName3'> 

Open in new window

0
Comment
Question by:chaitu chaitu
  • 8
  • 5
13 Comments
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36814386
Check this.
Demo http://jsfiddle.net/9Vj45/1/
<html>
<head>
<title>Kiran Test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>

<script>

$(document).ready(function(){
  
  var jsonObj = [{"LastName1":"l1","FirstName1":"f1","LastName2":"l2","FirstName2":"f2"}]
  
  $.each(eval(jsonObj),function(i,obj){
    
    $("#lastName1").val(obj.LastName1);
    $("#firstName1").val(obj.FirstName1);
    
    $("#lastName2").val(obj.LastName2);
    $("#firstName2").val(obj.FirstName2);
  });
  
});



</script>
</head>



<input type='text' id='lastName1'> 
<input type='text' id='firstName1'> 

<input type='text' id='lastName2'> 
<input type='text' id='firstName2'> 

<input type='text' id='lastName3'> 
<input type='text' id='firstName3'> 


</body>
</html>

Open in new window

0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 36814397
var jsonObj = [{"LastName1":"l1","FirstName1":"f1","LastName2":"l2","FirstName2":"f2"}]

i think i need to chang JSON string format  .with this format  every time i will get length is 1 but actually user added two names.
0
 
LVL 17

Accepted Solution

by:
sonawanekiran earned 500 total points
ID: 36814434
Yes. Then json format should be

[{"LastName":"l1","FirstName":"f1"},{"LastName":"l2","FirstName":"f2"}]

Then check below code
<html>
<head>
<title>Kiran Test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>

<script>

$(document).ready(function(){
  
  var jsonObj = [{"LastName":"l1","FirstName":"f1"},{"LastName":"l2","FirstName":"f2"}]
  
  $.each(eval(jsonObj),function(i,obj){
    
    $("#lastName" + (i+1)).val(obj.LastName);
    $("#firstName" + (i+1)).val(obj.FirstName);
    
    
    
  });
  
});



</script>
</head>



<input type='text' id='lastName1'> 
<input type='text' id='firstName1'> 

<input type='text' id='lastName2'> 
<input type='text' id='firstName2'> 



</body>
</html>

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 20

Author Comment

by:chaitu chaitu
ID: 36814673
JSONObject json = new JSONObject();
json.put("FirstName1", "f1");
json.put("LastName1",  "l1");
json.put("FirstName2", "f2");
json.put("LastName2",  "l2");

formBean.setObject(json.toString());

var jsonObj = [{"LastName1":"l1","FirstName1":"f1","LastName2":"l2","FirstName2":"f2"}]

presently  am getting json string  in above format using java code above;how to construct in desired format like below one?do i need to construct manually in java ?

[{"LastName":"l1","FirstName":"f1"},{"LastName":"l2","FirstName":"f2"}]
0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36814724
Sorry I am not java guy but you can try like this

JSONArray jArray = new JSONArray();
while (itr.hasNext()) {
   JSONObject json = new JSONObject();
   int objId = itr.next();
  json.put("FirstName1", "f1");
 json.put("LastName1",  "l1");
   jArray.put(json);
}

results = jArray.toString();
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 36890005
why undefined is coming in below JSON string?

  var jsonObj =[{"SuppFirstName1":"f1","SuppLastName1":"l1"},{"SuppFirstName2":"f2","SuppLastName2":"l2"}];


alert(jsonObj.length)
 $.each(eval(jsonObj),function(i,obj){
alert(obj.SuppLastName1)

    if(typeof(obj.SuppLastName1) !='undefined')
    $("#lastName1").val(obj.SuppLastName1);
       if(typeof(obj.SuppFirstName1) !='undefined')
    $("#firstName1").val(obj.SuppFirstName1);
   
    $("#lastName2").val(obj.SuppLastName2);
    $("#firstName2").val(obj.SuppFirstName2);
  });
 
0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36890032
Because SuppLastName1 property is in first object and there is no property SuppLastName1 in 2nd object. In second object there is SuppLastName2 property
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 36890097
if i dont iterate then values are not coming.

$(document).ready(function(){
 
  var jsonObj =eval([{"SuppFirstName1":"f1","SuppLastName1":"l1"},{"SuppFirstName2":"f2","SuppLastName2":"l2"}]);

// $.each(eval(jsonObj),function(i,obj){


    $("#lastName1").val(obj.SuppLastName1);
    $("#firstName1").val(obj.SuppFirstName1);
   
    $("#lastName2").val(obj.SuppLastName2);
    $("#firstName2").val(obj.SuppFirstName2);
 // });
 
});
0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36890132
What you want exactly??
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 36890141
without iteration also i think i should get values because i used eval function for JSON String.

var jsonObj =eval([{"SuppFirstName1":"f1","SuppLastName1":"l1"},{"SuppFirstName2":"f2","SuppLastName2":"l2"}]);



    $("#lastName1").val(obj.SuppLastName1);
    $("#firstName1").val(obj.SuppFirstName1);
   
    $("#lastName2").val(obj.SuppLastName2);
    $("#firstName2").val(obj.SuppFirstName2);
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 36890170
now a days thinking too much.i must iterate the JSON object to get the values.I thought without iteration we can get all values.
0
 
LVL 20

Author Closing Comment

by:chaitu chaitu
ID: 36890177
thanks for the help and it seems  harrassed a little bit and also confused you and myself as well.hav a nice day.
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 36890181
[{"LastName":"l1","FirstName":"f1"},{"LastName":"l2","FirstName":"f2"}]

for above format must iterate.

[{"LastName":"l1","FirstName":"f1","LastName":"l2","FirstName":"f2"}]

for above format need not to iterate.

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question