Home Web Front-end JS Tutorial Conversion between json objects and string objects in JavaScript_javascript skills

Conversion between json objects and string objects in JavaScript_javascript skills

May 16, 2016 pm 05:45 PM
json object Convert

json object
Copy code The code is as follows:

var json = { aa:true,bb:true};
var json1 = {aa:'b',bb:{cc:true,dd:true}};

1: js Manipulate json object
Copy code The code is as follows:

for(var item in json ){
alert(item); //The result is aa, bb, the type is string
alert(typeof(item));
alert(eval("json." item)); //The result is true, the true type is boolean
eval(("json." item "=false;")); //Change the value of the json object
}

2 : Method to convert json object into String object
Copy code The code is as follows:

/**
* json object to string format
*/
function json2str(o) {
var arr = [];
var fmt = function(s) {
if (typeof s == 'object' && s != null) return json2str(s);
return /^(string|number)$/.test(typeof s) ? "'" s "'" : s;
}
for ( var i in o) arr.push("'" i "':" fmt(o[i]));
return '{' arr.join(',') '}';
}

3: Convert string object to json object
Copy code The code is as follows :

function stringToJson(stringValue)
{
eval("var theJsonValue = " stringValue);
return theJsonValue;
}

4: Method to convert json array into String object (requires the above method)
Copy code The code is as follows:

function JsonArrayToStringCfz(jsonArray)
var JsonArrayString = "[";
for(var i=0;i JsonArrayString=JsonArrayString JsonToStringCfz(jsonArray[i]) ",";
}
JsonArrayString = JsonArrayString.substring(0,JsonArrayString.length-1) "]";
return JsonArrayString;
}

5: Use json.js json to string
Copy code Code As follows:


<script> <br>var date = {myArr : ["a" , " b" , "c" , "d"] , count : 4}; <br>var str = JSON.stringify(date); <br>alert(str); <br></script>
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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 get all keys of a JSON object using GSON in Java? How to get all keys of a JSON object using GSON in Java? Aug 30, 2023 pm 11:45 PM

AGson isalibrarythatcanbeusedtoparseJavaobjectstoJSON andvice-versa.ItcanalsobeusedtoconvertaJSONstringtoanequivalentJavaobject.InordertoparsejavaobjecttoJSONorJSONtojavaobject,weneedtoimportcom.google.gson packageintheJava

How can we merge two JSON objects in Java? How can we merge two JSON objects in Java? Aug 26, 2023 am 08:01 AM

JSON is a lightweight data exchange format, and the format of JSON is a key-value pair. JSONObject can parse text in a string to generate map-like objects and supports the java.util.Map interface. We can merge two JSON objects in Java using org.json.simple.JSONObject. We can merge two JSON objects using the putAll() method in the program below (inherited from the interface java.util.Map). Example importjava.util.Date;importorg.json.simple.JSONObject;publicclass

How to use JsonConfig in Java to convert bean to JSON object and exclude certain properties? How to use JsonConfig in Java to convert bean to JSON object and exclude certain properties? Sep 01, 2023 pm 06:37 PM

The JsonConfig class is a utility class that helps configure the serialization process. We can use the setExcludes() method of the JsonConfig class to convert a bean into a JSON object and exclude some of its properties, and pass this JSON configuration instance to the parameter of the static method fromObject() of JSONObject. SyntaxpublicvoidsetExcludes(String[]excludes)Inthebelowexample,wecanconvertbeantoaJSONobjectbyexc

How to convert HTML to PDF How to convert HTML to PDF Feb 24, 2024 am 08:36 AM

Converting HTML to PDF is one of the common requirements in web development. Below you will learn how to implement this functionality using specific code examples. First of all, what needs to be introduced is a commonly used open source library-pdfmake. pdfmake is a JavaScript library for generating PDFs that can specify the structure, style, and content of PDFs by using JSON objects. Before using it, we need to install it via npm: npminstallpdfmake After the installation is complete, I

How to convert a bean to JSON object using exclude filter in Java? How to convert a bean to JSON object using exclude filter in Java? Aug 18, 2023 pm 07:05 PM

The serialization process can be configured using the JsonConfig class. We can use JsonConfig's setJsonPropertyFilter() method to set the property filter when serializing to JSON. We need to implement a custom PropertyFilter class by overriding the apply() method of the PropertyFilter interface. Returns true if the attribute will be filtered out, false otherwise. Syntax publicvoidsetJsonPropertyFilter(PropertyFilterjsonPropertyFilter)Exampleimp

Detailed explanation of the implementation method of converting Java array into List Detailed explanation of the implementation method of converting Java array into List Dec 23, 2023 am 09:21 AM

Detailed explanation of how to convert a Java array into a List In Java programming, we often encounter the need to convert an array into a List. Java provides a variety of flexible ways to implement this operation. This article will introduce several commonly used methods in detail and give specific code examples. Using the asList method of the Arrays tool class The Arrays tool class is a tool class provided by Java for operating arrays. The asList method can convert an array into the corresponding List. The following is used

Tips for quickly converting Java arrays into Lists Tips for quickly converting Java arrays into Lists Dec 23, 2023 am 11:26 AM

Tips for quickly converting Java arrays into Lists require specific code examples. In Java programming, we often encounter the need to convert arrays into Lists. Using List can operate elements more flexibly and provides more methods and functions. The following will introduce several techniques for quickly converting Java arrays into Lists, including using the asList() method of the Arrays class, using the ArrayList constructor, and using the Stream stream in Java8. Method 1: Use Arr

Java code: How to convert array to List Java code: How to convert array to List Dec 23, 2023 pm 01:27 PM

How to use Java code to convert an array into a List. In Java, converting an array into a List is a very common operation. This conversion can be achieved by using Java's own Arrays class or using the StreamAPI introduced in Java 8. Specific code examples are given below. Method 1: Use the asList method of the Arrays class. The Arrays class is a tool class provided by Java for operating arrays. The asList method can convert the array into L

See all articles