?
Solved

how to display java JSON array in script

Posted on 2011-09-28
19
Medium Priority
?
375 Views
Last Modified: 2013-11-08
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>
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
  • 10
  • 9
19 Comments
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36715477
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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 36715571
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
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36715583
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 20

Author Comment

by:chaitu chaitu
ID: 36715592
getting data from jsp ;length is coming as 1;my assumption is length should come as 2
0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36715597
Show the data you are getting
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 36715604
[{"LastName1":"iouououououo","FirstName1":"fhhfhfhfh"}]
0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36715625
You are wrong. Json object length is 1. And that object has 2 properties (attributes)
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 36715638
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
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36715668
Have you tried this


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

Open in new window

0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 36715718
yes.still getting undefined
0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36715731
What is type your getting from JSP OR check like this

  alert(typeof jsonObj);
0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36715757
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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 36715835


string::"[{\"SuppLastName1\":\"iouououououo\",\"SuppFirstName1\":\"fhhfhfhfh\"}]"
0
 
LVL 17

Accepted Solution

by:
sonawanekiran earned 1000 total points
ID: 36715893
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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 36715903
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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 36715919
its working now like this data[0].SuppFirstName1 or ur code as well;

i think ur replcaing \\ with "" then its working..
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 36715926
what is the extact meaning of this??  jsonString.replace(/\\/,"");
   
0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36715927
At first you haven't provide the correct string whatever your are getting

0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36715935
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month10 days, 2 hours left to enroll

762 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