how to display java JSON array in script

the format is coming like this;
 how to display the length and values?
which method do i need among these eval,stringify,parseJSON ?

[{"LastName1":"iouououououo","FirstName1":"fhhfhfhfh"}]

<script>
var supps = JSON.stringify('<%=suppArray%>');
alert(supps.length)
</script>
LVL 20
chaitu chaituAsked:
Who is Participating?
 
Kiran SonawaneConnect With a Mentor Project LeadCommented:
This should work

$(document).ready(function(){
    var jsonString = "[{\"SuppLastName1\":\"iouououououo\",\"SuppFirstName1\":\"fhhfhfhfh\"}]";
    jsonString.replace(/\\/,"");
    alert(jsonString);
    $.each(jQuery.parseJSON(jsonString),function(i,obj){
     
     alert(obj.SuppLastName1);
    });
     
});
0
 
Kiran SonawaneProject LeadCommented:
Check this
<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":"iouououououo","FirstName1":"fhhfhfhfh"}]   
    
    // Get length
    alert(jsonObj.length);
    
    // Get Value
    
    $.each(eval(jsonObj),function(i,obj){
      
      alert(obj.LastName1 + " " + obj.FirstName1);
      
    });
     
});
</script>
</head>
<body>

</body>
</html>

Open in new window

0
 
chaitu chaituAuthor Commented:
i need to pass the json array into jsp scriplet in javascript.if i write like this am getting undefined alert message.

$(document).ready(function(){
    var jsonObj = '<%=suppArray%>';  
   
    // Get length
    alert(jsonObj.length);
   
    // Get Value
   
    $.each(eval(jsonObj),function(i,obj){
     
      alert(obj.LastName1 + " " + obj.FirstName1);
     
    });
     
});
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
Kiran SonawaneProject LeadCommented:
Check whether you are getting data from jsp or not like this

$(document).ready(function(){
    var jsonObj = '<%=suppArray%>';  
   
    // Get length
    alert(jsonObj);
   
    // Get Value
   
    $.each(eval(jsonObj),function(i,obj){
     
      alert(obj.LastName1 + " " + obj.FirstName1);
     
    });
     
});
0
 
chaitu chaituAuthor Commented:
getting data from jsp ;length is coming as 1;my assumption is length should come as 2
0
 
Kiran SonawaneProject LeadCommented:
Show the data you are getting
0
 
chaitu chaituAuthor Commented:
[{"LastName1":"iouououououo","FirstName1":"fhhfhfhfh"}]
0
 
Kiran SonawaneProject LeadCommented:
You are wrong. Json object length is 1. And that object has 2 properties (attributes)
0
 
chaitu chaituAuthor Commented:
var jsonObj = '<%=suppArray%>';   --when you iterate then length is coming but data is not coming.


var jsonObj =[{"LastName1":"iouououououo","FirstName1":"fhhfhfhfh"}] --when you iterate then data is coming.
0
 
Kiran SonawaneProject LeadCommented:
Have you tried this


$.each(jQuery.parseJSON(jsonObj),function(i,obj){
      alert(obj.LastName1);
    });

Open in new window

0
 
chaitu chaituAuthor Commented:
yes.still getting undefined
0
 
Kiran SonawaneProject LeadCommented:
What is type your getting from JSP OR check like this

  alert(typeof jsonObj);
0
 
Kiran SonawaneProject LeadCommented:
My observation is that
1) if the typeof is object you need use eval
2) if the typeof is string then you need  jQuery.parseJSON
0
 
chaitu chaituAuthor Commented:


string::"[{\"SuppLastName1\":\"iouououououo\",\"SuppFirstName1\":\"fhhfhfhfh\"}]"
0
 
chaitu chaituAuthor Commented:
its working now.let me check ur code.

    var jsonObj = '<%=suppArray%>';

var jobjson=JSON.parse(jsonObj);
var data = eval('(' + jsonObj + ')');

console.log(typeof jobjson+":typeof jobjson::::jobjson******SuppFirstName1***"+jobjson[0].SuppFirstName1+"****SuppLastName1*****"+jobjson[0].SuppLastName1) //string
console.log(data+":data::::SuppFirstName1::"+data[0].SuppFirstName1+"****SuppLastName1*****"+jobjson[0].SuppLastName1); //string

Open in new window

0
 
chaitu chaituAuthor Commented:
its working now like this data[0].SuppFirstName1 or ur code as well;

i think ur replcaing \\ with "" then its working..
0
 
chaitu chaituAuthor Commented:
what is the extact meaning of this??  jsonString.replace(/\\/,"");
   
0
 
Kiran SonawaneProject LeadCommented:
At first you haven't provide the correct string whatever your are getting

0
 
Kiran SonawaneProject LeadCommented:
String you are getting is "[{\"SuppLastName1\":\"iouououououo\",\"SuppFirstName1\":\"fhhfhfhfh\"}]" like this
which is not format of json string .
Correct format is "[{"LastName1":"iouououououo","FirstName1":"fhhfhfhfh"}] " ##### extra slashes
So I am replacing  slash with blank
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.