最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to convert All json values to String without iteration - Stack Overflow

programmeradmin4浏览0评论

I have JSON in which all the values have to be changed to string. The values may be a number, boolean, undefined or null.

{
    "obj1": [{
        "n1": "n",
        "n2": 1,
        "n3": true
    },
    {
        "n1": "n",
        "n2": 1,
        "n3": null
    }]
}

The expected result is all the values should be formatted as a string.

Example:

{
    "obj1": [{
        "n1": "n",
        "n2": "1",
        "n3": "true"
    },
    {
        "n1": "n",
        "n2": "1",
        "n3": "null"
    }]
}

By iterating through the JSON object we can do this, but is there any simpler way to do this.

I have JSON in which all the values have to be changed to string. The values may be a number, boolean, undefined or null.

{
    "obj1": [{
        "n1": "n",
        "n2": 1,
        "n3": true
    },
    {
        "n1": "n",
        "n2": 1,
        "n3": null
    }]
}

The expected result is all the values should be formatted as a string.

Example:

{
    "obj1": [{
        "n1": "n",
        "n2": "1",
        "n3": "true"
    },
    {
        "n1": "n",
        "n2": "1",
        "n3": "null"
    }]
}

By iterating through the JSON object we can do this, but is there any simpler way to do this.

Share Improve this question edited Dec 8, 2018 at 20:12 Studio KonKon 7906 silver badges15 bronze badges asked Dec 8, 2018 at 19:16 ashokashok 1,2683 gold badges27 silver badges71 bronze badges 3
  • 3 What do you mean with "without iteration"? Some iteration is needed, hidden or explicit – Christian Vincenzo Traina Commented Dec 8, 2018 at 19:19
  • You say you have JSON, so we are talking about one big string, right? – trincot Commented Dec 8, 2018 at 19:19
  • I know behind it needs iteration but any simpler way which it works behind – ashok Commented Dec 8, 2018 at 19:28
Add a ment  | 

3 Answers 3

Reset to default 14

You could take JSON.stringify with a replacer function and check if the values is a number, then take a stringed value, or just the value.

var object = { obj1: [{ n1: "n", n2: 1, n3: true }, { n1: "n", n2: 1, n3: null }] },
    json = JSON.stringify(object, (k, v) => v && typeof v === 'object' ? v : '' + v);

console.log(json);
console.log(JSON.parse(json));

You could do it with Json.stringify() method

for example:

var object = { obj1: [{ n1: "n", n2: 1, n3: true }, { n1: "n", n2: 1, n3: null }] };

and to see the result, use Json.stringify()

console.log(JSON.stringify(object, (key, value) => value ? value.toString() : value));
const obj = {
    "obj1": [{
        "n1": "n",
        "n2": 1,
        "n3": true
    }, {
        "n1": "n",
        "n2": 1,
        "n3": null
    }]
};
const text = JSON.stringify(obj)
const newObj = text.replace(/:([^"[{][0-9A-Za-z]*)([,\]\}]?)/g, ':\"$1\"$2')
console.log(newObj);
/*
{"obj1":[{"n1":"n","n2":"1","n3":"true"},{"n1":"n","n2":"1","n3":"null"}]}
*/

// Em, let's format it
console.log(JSON.stringify(JSON.parse(newObj), null, 2));
/*
{
  "obj1": [
    {
      "n1": "n",
      "n2": "1",
      "n3": "true"
    },
    {
      "n1": "n",
      "n2": "1",
      "n3": "null"
    }
  ]
}
*/
发布评论

评论列表(0)

  1. 暂无评论