纯字符串是有效的JSON吗? [英] Is a plain string valid JSON?

查看:192
本文介绍了纯字符串是有效的JSON吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

纯字符串是有效的JSON还是必须有一个对象?

Is a plain string valid JSON or does there have to be an object?

例如:

"morpheus"

与之相对:

{
    "name": "morpheus"
}

推荐答案

重要

这个答案曾经说不,JSON的第一个字符必须是{[.

Important

This answer once said No, the first character of the JSON must be a { or a [.

在我写这篇文章的时候,我正在用Python对其进行测试.在Python(至少是2.7.x)中,json.loads("a")给出了一个错误,这意味着纯字符串在那里不是有效的JSON.

At the time I wrote that, I was testing it with Python. In Python (2.7.x at least), json.loads("a") gives an error, which means that a plain string is not valid JSON there.

其他人正确地指出,不能说纯字符串不是有效的JSON.有关更合适的答案,请参见此问题.

It has been rightfully pointed out by others that it cannot be said that a plain string is not valid JSON. See this question for a more appropriate answer.

这时我只能说这取决于您的环境.在javascript中可能有效,在python中可能无效,等等,等等.

At this time all I can say is that it depends on your environment. In javascript it may be valid, in python it may not be, etc, etc.

JSON代表JavaScript对象表示法

JSON stands for JavaScript Object Notation

这是来自官方网站

JSON建立在两个结构上:

JSON is built on two structures:

名称/值对的集合.在各种语言中,这是 实现为对象,记录,结构,字典,哈希表,键控 列表或关联数组.值的有序列表.多数情况 语言,这可以实现为数组,向量,列表或序列. 这些是通用数据结构.几乎所有现代编程 语言以一种或另一种形式支持它们.有道理的是 可以与编程语言互换的数据格式 基于这些结构.

A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array. An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence. These are universal data structures. Virtually all modern programming languages support them in one form or another. It makes sense that a data format that is interchangeable with programming languages also be based on these structures.

在JSON中,它们采用以下形式:

In JSON, they take on these forms:

对象是一组无序的名称/值对.一个对象开始 以{(左括号)结尾,以}(右括号)结尾.每个名称是 后跟:(冒号),名称/值对之间用分隔, (逗号).

An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).

记下我加粗的文字.

因此,并且JSON是JS Object 表示法,这意味着name:value对的JSON表示必须始终采用

Because of that, and JSON being JS Object Notation, it is implied that a JSON representation of a name:value pair must always be in the form of

{
  "name": "value"
}

请注意,您也可以将根对象"设置为列表

Note that you can also make the 'root object' a list

[
  {
    "name1": "value1"
  },
  {
    "name2": "value2"
  }
]

这基本上意味着JSON包含多个对象.

This basically means that the JSON contains more than one object.

正如Sunny R Gupta指出的那样,这也是有效的JSON

As Sunny R Gupta pointed out, this is also valid JSON

[
  "this",
  "is",
  "valid"
]

请注意,这是有效的,因为字符串的格式不是"name":"value"而是字符串.考虑到这一点,您的示例中的有效选项将是

Note that this works because the strings are not in the form "name":"value" but just strings. Taking that into consideration valid options for your example would be

{
  "name": "Morpheus"
}

[
  "morpheus"
]

JSON 的第一个字符 必须是{[

The first character of the JSON must be a { or a [

这篇关于纯字符串是有效的JSON吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆