这个简单的字符串是否被视为有效JSON? [英] Is this simple string considered valid JSON?

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

问题描述

我在SO中看到了这么多复杂的问题,不管某个复杂的结构是否被认为是有效的JSON。



但另一端是什么呢?光谱?

 12345

以上是否有效JSON?

解决方案

它是有效的JSON语法,代表JSON 的。是否是有效的JSON 文本(用于指代JSON生成器的完整输出的正式术语)取决于上下文。



2006年发布的Douglas Crockford的


I've seen so many complicated questions in SO whether or not some complicated structure is considered to be valid JSON.

But what about something on the other end of the spectrum?

"12345"

Is the above valid JSON?

解决方案

It is valid JSON syntax representing a JSON value. Whether or not is a valid JSON text (the formal term used to refer to the complete output of a JSON generator) depends on the context.

In Douglas Crockford's RFC 4627 published in 2006 (which defines the application/json internet media type):

A JSON text is a serialized object or array.

Therefore it would not be legal to use that simple string as the complete body of an HTTP response with the internet media type application/json.

However, JSON specification has been updated in RFC 7159 published in 2014, as well as in the edition 5.1 of ECMA-262 and in ECMA-404. The new specifications do not have the above limitation, and so would consider that simple string to be a valid JSON text. In RFC 7159:

A JSON text is a serialized value. Note that certain previous specifications of JSON constrained a JSON text to be an object or an array. Implementations that generate only objects or arrays where a JSON text is called for will be interoperable in the sense that all implementations will accept these as conforming JSON texts.


A JSON value, from json.org:

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

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