简体   繁体   English

将具有对象和数组的JSON转换为String

[英]Convert JSON with objects and arrays to String

I have a JSON object which looks like this: 我有一个看起来像这样的JSON对象:

    {
        files: ['test.mp4'],
        name: ['testFile'],
        hints: ['%YES%_%MAYBE%_%NO%']
    }

And I need to convert it to a String so the output looks like this: 而且我需要将其转换为字符串,以便输出如下所示:

[{files=test, name=testFile, hints= %YES%_%MAYBE%_%NO%}]

Is this possible to achieve in Node JS? 这可以在Node JS中实现吗? Thanks 谢谢

I tried the following: 我尝试了以下方法:

var x = {
    files: ['test.mp4'],
    name: ['testFile'],
    hints: ['%YES%_%MAYBE%_%NO%']
}
console.log(JSON.stringify(x));

But the output looks like this: 但是输出看起来像这样:

{"files":["test.mp4"],"name":["testFile"],"hints":["%YES%_%MAYBE%_%NO%"]}

Still with the square brackets. 仍带有方括号。 I may not 100% know the keys and values in the object above. 我可能不是100%知道上面对象中的键和值。

Try JSON.stringify(obj) 试试JSON.stringify(obj)

then you get a string with quotes etc. 然后你得到一个带引号的字符串等。

JavaScript has JSON.stringify() method which can convert an object into string: JavaScript具有JSON.stringify()方法,该方法可以将对象转换为字符串:

var x = {
    files: ['test.mp4'],
    name: ['testFile'],
    hints: ['%YES%_%MAYBE%_%NO%']
}
console.log(JSON.stringify(x));
// result: '{"files":["test.mp4"],"name":["testFile"],"hints":["%YES%_%MAYBE%_%NO%"]}'

This will result in a string which can be transformed back to JS object with JSON.parse() method. 这将导致一个字符串,可以使用JSON.parse()方法将其转换回JS对象。 If you still want to remove all brackets and quotes, you can simply use JavaScript's replace() method (replacing characters [ , ] , and " with empty string), but this will replace those characters in all your values (if any) and will result in (sort of) non-reusable string. 如果仍要删除所有方括号和引号,则只需使用JavaScript的replace()方法(用空字符串替换字符[]" ),但这将替换所有值(如果有)中的那些字符,并且将导致(某种)不可重用的字符串。

TL;DR Don't do this unless you absolutely have to (ie. you're dealing with a messed up API written by someone else that must have the data in this format) TL; DR除非绝对必要,否则不要这样做(即,您正在处理必须由这种格式的数据的其他人编写的混乱的API)

If you want exactly the format listed in your question, then you're going to have to write your own stringify function that recursively walks through the object you pass to it and applies whatever rules you want to use. 如果您确切想要问题中列出的格式,那么您将必须编写自己的stringify函数,该函数以递归方式遍历传递给它的对象并应用您想使用的任何规则。 You will have to consider all the possible permutations of your object and spell out those rules. 您将必须考虑对象的所有可能排列并阐明这些规则。

For example, you've converted arrays with single elements in the initial object into strings - what happens if there is more than one element in the array? 例如,您已将初始对象中具有单个元素的数组转换为字符串-如果数组中有多个元素会发生什么? Should it be delimited by a comma or some other character? 应该用逗号或其他字符分隔吗? Should we just throw away elements after the first? 我们应该在第一个元素之后扔掉元素吗?

And once you've written the stringify function, you'll also have to write the corresponding parse function to reverse it. 并且,一旦编写了stringify函数,就还必须编写相应的parse函数以将其反转。 And it should be mentioned that in your example, you're throwing away information (eg. the file extension on the .mp4 file) - how are you going to handle that? 值得一提的是,在您的示例中,您正在丢弃信息(例如.mp4文件的文件扩展名)-您将如何处理?

A much, much better way to approach this would be to do what other people have suggested here: use JSON.stringify and rewrite your code to use standard JSON objects. 一种更好的方法是执行其他人在这里建议的操作: 使用JSON.stringify并重写代码以使用标准JSON对象。 Why? 为什么? Because the format is well documented and well understood and because the functions to convert are well tested. 因为该格式有充分的文档记录和理解,并且因为转换的功能已经过测试。 You will save yourself a whole lot of time and pain if you don't try to reinvent the wheel here . 如果不尝试在这里重新发明轮子,您将节省很多时间和痛苦

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM