如何使用重复键[duplicate]创建JSON对象

时间:2022-11-23 08:59:32

This question already has an answer here:

这个问题已经有了答案:

I'm working on a SailsJS project and I need to create a json object for my ORM search to work, This is how the search should be performed

我正在做一个SailsJS项目,我需要为ORM搜索创建一个json对象,这是搜索应该执行的方式

Venue.find({
            is_published: true,
            restaurant_services: {
                contains: '"delivery":"1"',
                contains: '"takeout":"1"'
            },
            restaurant_specialties: {
                contains: '"breakfast":"1"',
            }
        }).exec

So as you may see the JSON object inside the Find() is the one O need to create and the values inside has duplicate keys.

因此,如您所见,Find()中的JSON对象是需要创建的对象,其中的值具有重复的键。

2 个解决方案

#1


2  

You can't. Your should try using something like this instead:

你不能。你应该试着用这样的方法:

Venue.find({
            is_published: true,
            restaurant_services: {
                contains:  ['"delivery":"1"','"takeout":"1"']
            },
            restaurant_specialties: {
                contains: [ '"breakfast":"1"' ]
            }
        }).exec

Or this:

或:

Venue.find({
            is_published: true,
            restaurant_services: {
                contains: {"delivery":"1","takeout":"1"}
            },
            restaurant_specialties: {
                contains: { "breakfast":"1" }
            }
        }).exec

#2


1  

The problem is that the {... } Jason represents a map and therefore can't have duplicate keys. Although duplicate keys are strictly not Syntax errors but they are going to able also not going to work as expected with browsers or json libraries. If you can't change the syntax for your json object then you will have to produce that json by string concatenation instead of the normal Javascript type approach.

问题是{…} Jason表示一个映射,因此不能有重复的键。虽然重复键严格来说不是语法错误,但是它们也不能在浏览器或json库中正常工作。如果不能更改json对象的语法,则必须通过字符串连接来生成json,而不是使用普通的Javascript类型方法。

#1


2  

You can't. Your should try using something like this instead:

你不能。你应该试着用这样的方法:

Venue.find({
            is_published: true,
            restaurant_services: {
                contains:  ['"delivery":"1"','"takeout":"1"']
            },
            restaurant_specialties: {
                contains: [ '"breakfast":"1"' ]
            }
        }).exec

Or this:

或:

Venue.find({
            is_published: true,
            restaurant_services: {
                contains: {"delivery":"1","takeout":"1"}
            },
            restaurant_specialties: {
                contains: { "breakfast":"1" }
            }
        }).exec

#2


1  

The problem is that the {... } Jason represents a map and therefore can't have duplicate keys. Although duplicate keys are strictly not Syntax errors but they are going to able also not going to work as expected with browsers or json libraries. If you can't change the syntax for your json object then you will have to produce that json by string concatenation instead of the normal Javascript type approach.

问题是{…} Jason表示一个映射,因此不能有重复的键。虽然重复键严格来说不是语法错误,但是它们也不能在浏览器或json库中正常工作。如果不能更改json对象的语法,则必须通过字符串连接来生成json,而不是使用普通的Javascript类型方法。