JSON.parse()和JSON.stringify()的用法

JSON.parse()和JSON.stringify()的用法

JSON.parse()是用于从一个字符串中解析出json对象,如下所示

var str = '{"name":"flsummer","age":"23"}'

JSON.parse(str)

结果:Object {name: "flsummer", age: "23"}

注意:单引号必须写在{}外,每个属性名都必须用双引号,否则会抛出异常。

 

JSON.stringify()用于从一个对象解析出字符串,如

var a = {a:1,b:2};

JSON.stringify(a);

结果:"{"a":1,"b":2}"

 

let vm = new Vue({
    el: '#app',
    delimiters: ['[[', ']]'],
    data: {
        username: getCookie('username'),
        is_show_edit: false,
        form_address: {
            receiver: '',
            province_id: '',
            city_id: '',
            district_id: '',
            place: '',
            mobile: '',
            tel: '',
            email: '',
        },

        provinces: [],
        cities: [],
        districts: [],

     addresses: JSON.parse(addresses),

default_address_id: default_address_id, editing_address_index:
'', edit_title_index: '', new_title: '', error_receiver: false, error_place: false, error_mobile: false, error_tel: false, error_email: false, },

 

    <script type="text/javascript">
        let addresses = "{{ addresses|safe }}";
        let default_address_id = "{{ 'default_address_id' }}";
    </script>

 

posted @ 2020-07-20 16:11  sewen  Views(357)  Comments(0Edit  收藏  举报