1

The following is my code for converting Json string to object. Please suggest me any better and more reliable way.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"
    import="com.pks.UserBean"
    %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<title>Insert title here</title>
</head>
<body>
    This page contains data
            <% 
            UserBean bean = (UserBean)(session.getAttribute("currentSessionUser"));
            %>
            s<h1><%String a = bean.getUserName(); %></h1>
            <%= a %>

            <script type="text/javascript">
            alert("hELLO WORLD");
            var my = '<%= a %>'
            alert(my);

            var obj = jQuery.parseJSON('{"name":"John"}');
            alert( obj.name === "John" );

            </script>

</body>
</html>
abhishek varma
  • 45
  • 1
  • 1
  • 4
  • 1
    `JSON.parse()` ...? – jdphenix Nov 22 '16 at 02:53
  • 1
    jQuery's not reliable enough? – ChiefTwoPencils Nov 22 '16 at 03:27
  • 1
    Please remove all the server-side and HTML head element noise from your code. Also please note that the correct spelling is "JavaScript" and "JSON". There is no such thing as a "JSON object"; there are just JavaScript objects. There is no need to say "JSON string"; JSON is always a string. Anyway, if your code works, what is the question? SO is not a code-optimization service. By the way, there is no need for the `text` attribute on the `script` tag these days. –  Nov 22 '16 at 04:43
  • Duplicate of http://stackoverflow.com/questions/4935632/parse-json-in-javascript, or http://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object. –  Nov 22 '16 at 04:50
  • have you tried `JSON.parse('{"name":"John"}');` ? – Debug Diva Nov 22 '16 at 07:03

1 Answers1

0

Simply JSON.parse() method would suffice your requirement, no need of jQuery at all.

var jsonString = '{ "name" : "John" }';

var jsonObject = JSON.parse(jsonString);

console.log(jsonObject);

// The example you tried on your code.
alert(jsonObject.name === "John");
bharadhwaj
  • 2,059
  • 22
  • 35
  • Thanks mate !.. and also can you please help me on how to iterate over a list of Objects in Json and I want to displa them in table format in UI... – abhishek varma Nov 22 '16 at 04:41
  • @abhishekvarma If you have another question, please ask it as another question, not as a comment on an answer. Iterating is fundamental to JS and will be covered in any beginner tutorial, as would how to build a UI in JS. You should review those materials, then come back when you have specific questions. –  Nov 22 '16 at 04:47
  • Somebody down voted this answer, but I don't know why! I hope this solved the query. – bharadhwaj Nov 23 '16 at 05:53