Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript——Json字符串到对象的转换_Javascript_Json - Fatal编程技术网

JavaScript——Json字符串到对象的转换

JavaScript——Json字符串到对象的转换,javascript,json,Javascript,Json,下面是我将Json字符串转换为对象的代码。请给我推荐更好更可靠的方法 <%@ 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:/

下面是我将Json字符串转换为对象的代码。请给我推荐更好更可靠的方法

<%@ 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>
简单的JSON.parse方法就足以满足您的需求,根本不需要jQuery

var jsonString='{name:John}'; var jsonObject=JSON.parsejsonString; console.logjsonObject; //您在代码上尝试的示例。
alertjsonObject.name==John;JSON.parse…?jQuery不够可靠?请从代码中删除所有服务器端和HTML头元素噪声。另外请注意,正确的拼写是JavaScript和JSON。没有JSON对象这样的东西;只有JavaScript对象。不需要说JSON字符串;JSON总是一个字符串。不管怎样,如果你的代码有效,问题是什么?因此,这不是一个代码优化服务。顺便说一句,现在脚本标记上不需要text属性。重复的,或。您尝试过JSON.parse“{name:John};”吗?谢谢你,伙计!。。另外,你能帮助我如何迭代Json中的对象列表,我想在UI中以表格格式显示它们…@abhishekvarma如果你还有其他问题,请作为另一个问题提问,而不是作为对答案的评论。迭代是JS的基础,任何初学者教程都会介绍迭代,如何在JS中构建UI也是如此。你应该复习那些材料,然后有具体问题再回来。有人否决了这个答案,但我不知道为什么!我希望这解决了这个问题。