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

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

LVL 20
chaitu chaituAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Kiran SonawaneConnect With a Mentor Project LeadCommented:
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
 
Kiran SonawaneProject LeadCommented:
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
 
chaitu chaituAuthor Commented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
chaitu chaituAuthor Commented:
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
 
Kiran SonawaneProject LeadCommented:
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
 
chaitu chaituAuthor Commented:
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
 
Kiran SonawaneProject LeadCommented:
Because SuppLastName1 property is in first object and there is no property SuppLastName1 in 2nd object. In second object there is SuppLastName2 property
0
 
chaitu chaituAuthor Commented:
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
 
Kiran SonawaneProject LeadCommented:
What you want exactly??
0
 
chaitu chaituAuthor Commented:
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
 
chaitu chaituAuthor Commented:
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
 
chaitu chaituAuthor Commented:
thanks for the help and it seems  harrassed a little bit and also confused you and myself as well.hav a nice day.
0
 
chaitu chaituAuthor Commented:
[{"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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.