我想知道是否有办法使用Json.net一次读取一个json对象。目前,这里是我使用的代码,它可以工作,但是在流程读取器中加载整个文件,但不能一次解析一个对象。任何建议??
StreamReader streamReader = new StreamReader(@"Sample.json");
List<Member> mlist;
using (JsonReader reader = new JsonTextReader(streamReader))
{
JsonSerializer serializer = new JsonSerializer();
mlist = serializer.Deserialize<List<Member>>(reader);
}
答案 0 :(得分:2)
我能够在你的评论和其他链接的帮助下找到我的问题的解决方案:
StreamReader streamReader = new StreamReader(@"C:\Sample.json");
using (JsonTextReader reader = new JsonTextReader(streamReader))
{
while (reader.Read())
{
if (reader.TokenType == JsonToken.StartObject)
{
// Load each object from the stream and do something with it
JObject obj = JObject.Load(reader);
JsonSerializer serializer = new JsonSerializer();
Member m = (Member)serializer.Deserialize(new JTokenReader(obj), typeof(Member));
}
}
}
}