如何在JSON对象的键中包含键值对?

时间:2019-06-04 07:00:12

标签: java json excel

我如何获取具有如下对象的json对象:-

   obj={Key1:
             Question:How are you?
             Answer:I am fine 
        Key2:
             Question:How old are you?
             Answer:I am 16 years
       }

3 个答案:

答案 0 :(得分:0)

如果您实际上不需要key1,key2,...别名,而只想获取多个数据(“问题和答案”),则可以使用以下JSON。

[
  {
    "Answer": "I am fine",
    "Question": "How are you?"
  },
  {
    "Answer": "I am fine",
    "Question": "How are you?"
  }
]

此外,您只需要在json数组中提供json字符串,就可以对其进行迭代。

如果您对此有任何疑问/疑虑,请告诉我。

谢谢!

答案 1 :(得分:-1)

像这样吗?

{
    "obj": {
        "Key1": {
            "Question": "How are you?",
            "Answer": "I am fine"
        },
        "Key2": {
            "Question": "How old are you?",
            "Answer": "I am 16 years"
        }
    }
}

答案 2 :(得分:-3)

您需要将键的值分配为json对象。

value = {"Question":"How are you?","Answer":"I am fine"}

obj = {"Key1":value}

这将导致

 {"Key1": {"Question": "How are you?", "Answer": "I am fine"}}