Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of javascript json garbled code

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to solve the javascript json garbled problem". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to solve the javascript json garbled problem".

Javascript json garbled solutions: 1, copy the json.js file on the json official website; 2, introduce json;3 in the HTML page, and change the coding mode to GB2312 through "charset=GB2312".

This article operating environment: windows7 system, javascript1.8.5 version, Dell G3 computer.

How to solve the javascript json garbled problem?

The use of javascript json (and the resolution of Chinese garbled code)

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

Then introduce it on the page and use it:

To test whether it works, you can write a jsp page, and then add a method to the button to pop up the processed json string.

For Chinese garbled code, change the coding mode to: GB2312.

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

Changes to the page:

Java code response returns the modified method:

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

Let's take a look at the code, which introduces the conversion between strings and json objects: (this example uses jquery, but a button click event is called, which you can change to onclick)

Insert title here button {margin:4px; cursor:pointer;} input {margin:4px; color:blue;} json / # button3 $("# button3") .click (function () {/ json object converted to j string-- toJSONString () / / string converted to json object-- eval ('('+ str +')') / / take the value point of an attribute in the json object (for example, myObject.bindings [0] .time) var myJSONObject = {"bindings": [{"nnn": "Spring", "time": "Mark", "add": "Beijing"}, {"ircEvent": "good", "method": "method", "regex": "Shanghai"}]} Alert ("json object takes attribute value\ n" + myJSONObject.bindings [0] .nnn); / / json object conversion to json string method: var myJSONtext=myJSONObject.toJSONString (); alert ("json object to string, toJSONString\ n" + myJSONtext); var myJSONtext2=JSON.stringify (myJSONObject); alert ("json object is converted to a string, using the global built-in object JSON.stringify\ n" + myJSONtext2) / / json string conversion to json object methods: var myJSONObject2 = eval ('('+ myJSONtext +')'); var myJSONObject3=myJSONtext.parseJSON (); var myJSONObject4=JSON.parse (myJSONtext); alert ("string conversion to json object, using eval\ n" + myJSONObject2.bindings [0] .time); alert ("string conversion to json object, using parseJSON\ n" + myJSONObject3.bindings [0] .add) Alert ("convert strings to json objects, using the global built-in object JSON.parse\ n" + myJSONObject4.bindings [0] .nnn); /}) Thank you for your reading, the above is the content of "how to solve the javascript json garbled problem", after the study of this article, I believe you have a deeper understanding of how to solve the javascript json garbled problem, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report