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

How to solve the javascript json garbled problem

Jan 18, 2022 pm 02:44 PM
javascript json Garbled characters

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the problem of garbled characters when importing Chinese data into Oracle? How to solve the problem of garbled characters when importing Chinese data into Oracle? Mar 10, 2024 am 09:54 AM

Title: Methods and code examples to solve the problem of garbled characters when importing Chinese data into Oracle. When importing Chinese data into Oracle database, garbled characters often appear. This may be due to incorrect database character set settings or encoding conversion problems during the import process. . In order to solve this problem, we can take some methods to ensure that the imported Chinese data can be displayed correctly. The following are some solutions and specific code examples: 1. Check the database character set settings In the Oracle database, the character set settings are

Performance optimization tips for converting PHP arrays to JSON Performance optimization tips for converting PHP arrays to JSON May 04, 2024 pm 06:15 PM

Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

How to deal with garbled characters in Linux terminal How to deal with garbled characters in Linux terminal Mar 20, 2024 pm 03:12 PM

How to deal with the problem of garbled characters in the Linux terminal. When using the Linux system, sometimes the text displayed in the terminal will be garbled. This brings inconvenience to us when using the terminal and needs to be dealt with in time. This article will introduce how to deal with some common Linux terminal garbled problems, and provide specific code examples. Problem 1: Garbled Chinese characters on the terminal. Garbled Chinese characters on the terminal are usually caused by incorrect character encoding settings on the terminal. We can solve this problem by modifying the terminal's character encoding settings. #View the current terminal

How do annotations in the Jackson library control JSON serialization and deserialization? How do annotations in the Jackson library control JSON serialization and deserialization? May 06, 2024 pm 10:09 PM

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

Strategies and techniques for solving Chinese garbled characters in Oracle database Strategies and techniques for solving Chinese garbled characters in Oracle database Mar 08, 2024 am 09:48 AM

Strategies and techniques for solving Chinese garbled characters in Oracle database. In actual database applications, many developers or administrators may encounter the problem of Chinese garbled characters in Oracle database. When the data in the database is garbled, it not only affects the correctness and readability of the data, but also brings many problems to the system. This article will combine specific code examples to introduce the strategies and techniques for solving Chinese garbled characters in Oracle database to help readers better understand and solve this problem. 1. The reason for garbled characters is inconsistent character sets: database connection and application

Tips and practices for handling Chinese garbled characters in databases with PHP Tips and practices for handling Chinese garbled characters in databases with PHP Mar 27, 2024 pm 05:21 PM

PHP is a back-end programming language widely used in website development. It has powerful database operation functions and is often used to interact with databases such as MySQL. However, due to the complexity of Chinese character encoding, problems often arise when dealing with Chinese garbled characters in the database. This article will introduce the skills and practices of PHP in handling Chinese garbled characters in databases, including common causes of garbled characters, solutions and specific code examples. Common reasons for garbled characters are incorrect database character set settings: the correct character set needs to be selected when creating the database, such as utf8 or u

How to avoid Chinese garbled characters in WordPress How to avoid Chinese garbled characters in WordPress Mar 05, 2024 pm 09:36 PM

How to avoid Chinese garbled characters in WordPress requires specific code examples. In the process of using WordPress websites, many users will encounter the problem of Chinese garbled characters. Garbled Chinese characters will cause trouble for users when reading and browsing the website, and may also affect the user experience and search engine optimization of the website. In this article, we will introduce some methods to solve the Chinese garbled problem in WordPress and provide specific code examples. Set the database character set: First, make sure the database character set is set correctly to support the

Quick tips for converting PHP arrays to JSON Quick tips for converting PHP arrays to JSON May 03, 2024 pm 06:33 PM

PHP arrays can be converted to JSON strings through the json_encode() function (for example: $json=json_encode($array);), and conversely, the json_decode() function can be used to convert from JSON to arrays ($array=json_decode($json);) . Other tips include avoiding deep conversions, specifying custom options, and using third-party libraries.

See all articles