Home > Web Front-end > JS Tutorial > body text

Examples of conversion operations between json objects and strings in js

小云云
Release: 2018-02-03 13:27:28
Original
1609 people have browsed it

This article mainly introduces the understanding of json objects and strings in js and the implementation methods of mutual conversion operations. It analyzes the functions of json objects and strings and the implementation techniques of mutual conversion operations in the form of examples. Friends in need can refer to it. Hope it helps everyone.


<script>
 var str="{&#39;strv&#39;:["+
       "{&#39;a&#39;:&#39;a11&#39;},"+
       " {&#39;a&#39;:&#39;b222&#39;}"+
     " ]}";
 //如果放在一行更清楚:var str="{&#39;strv&#39;:[{&#39;a&#39;:&#39;a11&#39;}, {&#39;a&#39;:&#39;b222&#39;} ]}";
 var str2=eval(&#39;(&#39;+str+&#39;)&#39;);
// alert(str2.strv[1].a);
 var jsonob={&#39;jsonv&#39;:[
         {&#39;j&#39;:&#39;j111&#39;},
         {&#39;j&#39;:&#39;j222&#39;}
       ]};
//如果放在一行更清楚:var jsonob={&#39;jsonv&#39;:[ {&#39;j&#39;:&#39;j111&#39;}, {&#39;j&#39;:&#39;j222&#39;} ]};
// alert(jsonob.jsonv[1].j);
 /*
var  str="{&#39;strv&#39;:[{&#39;a&#39;:&#39;a11&#39;}, {&#39;a&#39;:&#39;b222&#39;} ]}";
var jsonob={&#39;jsonv&#39;:[ {&#39;j&#39;:&#39;j111&#39;}, {&#39;j&#39;:&#39;j222&#39;} ]};
这样我们可以看出json对象和字符串是不一样的!
字符串就是字符串,json对象是一个对象。
虽然其内容都是我们看到的json格式,但是两者是有区别的。
我们想取出其中某个属性的值,其实是对json对象操作的(你能取出对象的属性值,但不能取出字符串的属性值,字符串没属性。)。
所以如果你拿到的是对象,那你就直接可以取值了,
例如jsonob.jsonv[1].j。
如果你拿到的是字符串,那你必须先转化成对象才可以取值,
字符串转对象有几种方法,js本身自带的是var str2=eval(&#39;(&#39;+str+&#39;)&#39;);
这里的str2就是对象了。如果你引入了json.js文件,还可以使用其中的
方法,这些都已经封装好了。
总结:json只是一种格式。符合这种格式的可以是json对象,也可以是字符串。
要取属性值只能从对象里取。所以需要两者的转化。
js自带的转化方法:
 字符串转成json对象:
   var str="{&#39;strv&#39;:[{&#39;a&#39;:&#39;a11&#39;}, {&#39;a&#39;:&#39;b222&#39;} ]}";//字符串
   var jsonObject=eval(&#39;(&#39;+str+&#39;)&#39;);//json对象
 json对象转成字符串:
   js本身没有这样的方法,你需要自己写这样的方法,才能转化。
从上可知,js本身只可以将字符串转成json对象,
但不可以将json对象转成字符串。其实我们可以用
json.js里的方法,而且更简单。这时你要引入json.js文件。
json.js里的方法:
 字符串转成json对象:
     var str="{&#39;strv&#39;:[{&#39;a&#39;:&#39;a11&#39;}, {&#39;a&#39;:&#39;b222&#39;} ]}";//字符串
  转成json对象方法1: var myJSONObject1=str.parseJSON();
  转成json对象方法2: var myJSONObject2=JSON.parse(str);
 json对象转成字符串:
     var jsonob={&#39;jsonv&#39;:[ {&#39;j&#39;:&#39;j111&#39;}, {&#39;j&#39;:&#39;j222&#39;} ]};//json对象
  转成字符串方法1:var mystr1=jsonob.toJSONString();
  转成字符串方法2: var mystr2=JSON.stringify(jsonob);
 */
</script>
Copy after login

Through experiments, we should understand that json is actually a format, and js itself supports this format,

So you don’t need to introduce json. js can use json objects. We usually introduce json.js because it encapsulates json object and string conversion methods for our convenience (of course there are other methods).

Through this example we can better understand the difference between json objects and strings.

Related recommendations:

Detailed explanation of common js array operations and examples of mutual conversion between arrays and strings

The above is the detailed content of Examples of conversion operations between json objects and strings in js. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!