如何从json对象文字中删除\ n

时间:2016-06-08 07:30:44

标签: javascript json

当我编写console.log(输入)时,我得到以下代码,但我不想以这种方式格式化它:

var contacts = { json: ' "book": {\n        
"person": [\n 
{\n
    "firstName": "Jane",\n 
    "lastName": "Doe",\n
    "age": "25",\n
    "address": {\n
        "streetAddress": "21 2nd Street",\n
        "city": "Las Vegas",\n
        "state": "NV",\n
        "postalCode": "10021-3100"\n
    }\n
},\n
{\n
    "firstName": "Agatha",\n
    "lastName": "Doe",\n
    "age": "25",\n
    "address": {\n
        "streetAddress": "21 2nd Street",\n
        "city": "Las Vegas",\n
        "state": "NV",\n
        "postalCode": "10021-3100"\n
        }\n
    }\n
  ]\n
}' }

如何转换以下格式的上述代码?

var contacts = {
    "book": {
        "person": [
            {
                "firstName": "Jane",
                "lastName": "Doe",
                "age": "25",
                "address": {
                    "streetAddress": "21 2nd Street",
                    "city": "Las Vegas",
                    "state": "NV",
                    "postalCode": "10021-3100"
                }
            },
            {
                "firstName": "Agatha",
                "lastName": "Doe",
                "age": "25",
                "address": {
                    "streetAddress": "21 2nd Street",
                    "city": "Las Vegas",
                    "state": "NV",
                    "postalCode": "10021-3100"
                }
            }
        ]
    }
}

2 个答案:

答案 0 :(得分:0)

  

当我写console.log(input)时,我得到以下代码,但我不想以这种方式格式化

(我将假设input真的是contacts,这是你在问题中引用的内容。)

控制台格式化输出的方式几乎完全取决于控制台。你无法直接控制它。如果你给它一个对象(这是你用console.log(contacts)做的事情),控制台的实现将输出实现它的人认为合适的任何方式,并且它将因控制台而异 - 输出你在Firefox中看到开发工具与Chrome不同,它们都与NodeJS不同。

大多数控制台将以非常类似的方式输出字符串(尽管它们对字符串的引号处理方式各不相同),因此您可以将input转换为字符串并输出。一种方法是使用JSON.stringify

var contacts = {
    "book": {
        "person": [
            {
                "firstName": "Jane",
                "lastName": "Doe",
                "age": "25",
                "address": {
                    "streetAddress": "21 2nd Street",
                    "city": "Las Vegas",
                    "state": "NV",
                    "postalCode": "10021-3100"
                }
            },
            {
                "firstName": "Agatha",
                "lastName": "Doe",
                "age": "25",
                "address": {
                    "streetAddress": "21 2nd Street",
                    "city": "Las Vegas",
                    "state": "NV",
                    "postalCode": "10021-3100"
                }
            }
        ]
    }
};
console.log(JSON.stringify(contacts));

答案 1 :(得分:0)

json = ' "book": { "person": [ { "firstName": "Jane", "lastName": "Doe", "age": "25", "address": { "streetAddress": "21 2nd Street", "city": "Las Vegas", "state": "NV", "postalCode": "10021-3100" } }, { "firstName": "Agatha", "lastName": "Doe", "age": "25", "address": { "streetAddress": "21 2nd Street", "city": "Las Vegas", "state": "NV", "postalCode": "10021-3100" } } ] }'

var contact = JSON.parse(JSON.stringify(json)); console.log(contact);