C# 阅读完 JSON 内容后遇到的附加文本:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16765877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Additional text encountered after finished reading JSON content:
提问by Howard Hee
I am having some problems with create with JSON.Net. When I try to parse it, it gives me following error:
我在使用 JSON.Net 创建时遇到了一些问题。当我尝试解析它时,它给了我以下错误:
Additional text encountered after finished reading JSON content:
阅读完 JSON 内容后遇到的附加文本:
I tried validating it with http://json.parser.online.fr/and it says "SyntaxError: Unexpected token ,".
我尝试用http://json.parser.online.fr/验证它,它说“SyntaxError: Unexpected token ,”。
My JSON is as below:
我的 JSON 如下:
{"StaffID":"S01","StaffRank":"Manager"},{"StaffID":"S02","StaffRank":"Waiter"}
How to deserialize it?
如何反序列化它?
采纳答案by Kevin Schmid
You need to surround that with square brackets, which denotes that it's an array:
您需要用方括号将其括起来,这表示它是一个数组:
[{"StaffID":"S01","StaffRank":"Manager"},{"StaffID":"S02","StaffRank":"Waiter"}]
回答by dbc
As of Release 11.0.1, Json.NET now natively supports parsing comma-delimited JSON in the same way it supports parsing newline delimited JSON:
从11.0.1 版开始,Json.NET 现在原生支持解析逗号分隔的 JSON,就像它支持解析换行分隔的 JSON 一样:
New feature - Added support for reading multiple comma delimited values with
JsonReader.SupportMultipleContent.
新功能 - 添加了对读取多个逗号分隔值的支持
JsonReader.SupportMultipleContent。
Thus the answer to Line delimited json serializing and de-serializingby Yuval Itzchakovshould work here also. Define an extension method:
因此,Yuval Itzchakov对Line delimited json 序列化和反序列化的答案也应该在这里工作。定义扩展方法:
public static partial class JsonExtensions
{
public static IEnumerable<T> FromDelimitedJson<T>(TextReader reader, JsonSerializerSettings settings = null)
{
using (var jsonReader = new JsonTextReader(reader) { CloseInput = false, SupportMultipleContent = true })
{
var serializer = JsonSerializer.CreateDefault(settings);
while (jsonReader.Read())
{
if (jsonReader.TokenType == JsonToken.Comment)
continue;
yield return serializer.Deserialize<T>(jsonReader);
}
}
}
}
Then, given a data model created to hold an individual item in the comma-separated list such as:
然后,给定一个数据模型,用于保存逗号分隔列表中的单个项目,例如:
public class RootObject
{
public string StaffID { get; set; }
public string StaffRank { get; set; }
}
You can deserialize the JSON string shown like so:
您可以反序列化显示的 JSON 字符串,如下所示:
var jsonString = @"{""StaffID"":""S01"",""StaffRank"":""Manager""},{""StaffID"":""S02"",""StaffRank"":""Waiter""}";
var list = JsonExtensions.FromDelimitedJson<RootObject>(new StringReader(jsonString)).ToList();
This approach may be preferable when deserializing a very large sequence of comma-delimited objects from a large file, because it is not necessary to load the entire file into a stringthen add '['and ']'to the beginning and end. In Performance Tips: Optimize Memory UsageNewtonsoft recommends deserializing large files directly from a stream, so instead a StreamReadercan be passed into JsonExtensions.FromDelimitedJson()which will then stream through the file deserializing each object separately.
从一个大的反序列化文件逗号分隔对象的一个非常大的序列时,这种方法可能是优选的,因为它没有必要将整个文件加载到string再加入'['并']'于开始和结束。在性能提示:优化内存使用中,Newtonsoft 建议直接从流中反序列化大文件,因此StreamReader可以将 a 传入JsonExtensions.FromDelimitedJson()其中,然后将流过文件,分别反序列化每个对象。

