Home > Web Front-end > Front-end Q&A > How to solve the javascript json garbled problem

How to solve the javascript json garbled problem

藏色散人
Release: 2022-01-18 14:44:46
Original
5687 people have browsed it

Solution to javascript json garbled code: 1. Copy the json.js file on the json official website; 2. Introduce json into the HTML page; 3. Change the encoding method to GB2312 through "charset=GB2312".

How to solve the javascript json garbled problem

The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.

How to solve the garbled problem of javascript json?

Use of javascript json (and how to solve garbled Chinese garbled characters)

For js uses json, first go to the official website to copy the json.js file, the address is http://www.json.org/js.html

, and then import it on the page to use:

<script type="text/javascript" src="json.js"></script>
Copy after login

Test it Can it be used? You can write a jsp page, and then add a method to the button to pop up the processed json string.

For Chinese garbled characters, change the encoding method to: GB2312.

You can change it directly on the page or when the response returns.

How to modify the page:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
</head>
Copy after login

How to modify the java code response return:

response.setHeader("Cache-Control", "no-cache");
response.setContentType("text/json;charset=gb2312");
Copy after login

Let’s take a look at the code below, which introduces the mutual conversion of strings and json objects: (this The example uses jquery, but it calls a button click event. You can change it to onclick)

<%@ page language="java"  pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 

 



Insert title here
<script type="text/javascript" src="json.js"></script>

 

 

 

<script> /////////#button3 $("#button3").click(function () { ///json对象转化成j字符串--toJSONString() ///字符串转化成json对象--eval(&#39;(&#39; + str + &#39;)&#39;); ///取json对象里面某个属性的值用点(例如myObject.bindings[0].time) var myJSONObject = {"bindings": [ {"nnn": "春天", "time": "标示", "add": "北京"}, {"ircEvent": "好", "method": "方法", "regex": "上海"} ] }; alert("json对象取属性值\n"+myJSONObject.bindings[0].nnn ); ///json对象转化成json字符串方法: var myJSONtext=myJSONObject.toJSONString(); alert("json对象转化成字符串,toJSONString\n"+myJSONtext); var myJSONtext2=JSON.stringify(myJSONObject); alert("json对象转化成字符串,用全局的内置对象JSON.stringify\n"+myJSONtext2); ///json字符串转化成json对象方法: var myJSONObject2 = eval(&#39;(&#39; + myJSONtext + &#39;)&#39;); var myJSONObject3=myJSONtext.parseJSON(); var myJSONObject4=JSON.parse(myJSONtext); alert("字符串转化成json对象,用eval\n"+myJSONObject2.bindings[0].time ); alert("字符串转化成json对象,用parseJSON\n"+myJSONObject3.bindings[0].add ); alert("字符串转化成json对象,用全局的内置对象JSON.parse\n"+myJSONObject4.bindings[0].nnn ); ///////////////// }); </script>
Copy after login

Recommended study: "js Basic Tutorial"

The above is the detailed content of How to solve the javascript json garbled problem. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template