Solved

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

Posted on 2011-09-29
13
318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
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)

734 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