如何在json中存储多个值?

时间:2021-12-15 15:41:59

pretty simple question.

非常简单的问题。

I am trying to learn polymer and have made a survey. For one of the questions I would like to assign multiple values as the answer but for some reason I can only get it working with one value.

我正在努力学习聚合物并进行了调查。对于其中一个问题,我想分配多个值作为答案,但出于某种原因,我只能使用一个值。

"survey": [

        {
            "type": "picker",
            "question": "Please rate out of 5",
            "min": 1,
            "max": 5,
            "answer": 5
        }, 

This returns a positive result when the user selects 5 but when the user selects 1-4 it is incorrect.

当用户选择5时返回肯定结果,但当用户选择1-4时,它返回错误。

What I would like to do is store multiple values in the 'answer' field so that if the user selects 1-5 they all register as correct.

我想要做的是在'answer'字段中存储多个值,这样如果用户选择1-5,它们都会正确注册。

How can I do this? I have tried

我怎样才能做到这一点?我试过了

[1, 2, 3, 4, 5]

[1,2,3,4,5]

and it still doesn't work.

它仍然无法正常工作。

EDIT:

Here is the value html added in for polymer to create the slider.

以下是为聚合物添加的值html以创建滑块。

 <paper-slider id="slider" min="{{quiz.min}}" max="{{quiz.max}}" step="{{quiz.step || 1}}" immediateValue="{{value}}"></paper-slider>

2 个解决方案

#1


You can store 'answer' as an array in the json like

您可以将'answer'存储为json中的数组

     var survey = {
            "type": "picker",
            "question": "Please rate out of 5",
            "min": 1,
            "max": 5,
            "answer": [1,2,3]
        }

And whenever the user inputs something you just have to push the value like this

每当用户输入内容时,您只需要像这样推送值

console.log(survey); // Array has just 3 elements.
survey['answer'].push(4);
console.log(survey); // The value 4 would have been inserted.

How you are pushing the values?

你是如何推动价值观的?

Look at the Fiddle Link

看看小提琴链接

#2


Seems like it might work using children elements.

看起来它可能使用儿童元素。

"survey": [
    {
       "type": "picker",
       "question": "please rate out of 5",
       "answer": [
            {
                value: "1",
                value: "2",
                value: "3",
                value: "4",
                value: "5",
            }
        ]
    }
]

Reference: https://www.polymer-project.org/0.5/docs/polymer/binding-types.html

#1


You can store 'answer' as an array in the json like

您可以将'answer'存储为json中的数组

     var survey = {
            "type": "picker",
            "question": "Please rate out of 5",
            "min": 1,
            "max": 5,
            "answer": [1,2,3]
        }

And whenever the user inputs something you just have to push the value like this

每当用户输入内容时,您只需要像这样推送值

console.log(survey); // Array has just 3 elements.
survey['answer'].push(4);
console.log(survey); // The value 4 would have been inserted.

How you are pushing the values?

你是如何推动价值观的?

Look at the Fiddle Link

看看小提琴链接

#2


Seems like it might work using children elements.

看起来它可能使用儿童元素。

"survey": [
    {
       "type": "picker",
       "question": "please rate out of 5",
       "answer": [
            {
                value: "1",
                value: "2",
                value: "3",
                value: "4",
                value: "5",
            }
        ]
    }
]

Reference: https://www.polymer-project.org/0.5/docs/polymer/binding-types.html