C# 如何使用 JSON.net 解析 JSON 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9552409/
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
How do you parse a JSON file using JSON.net
提问by user972616
I am trying to read a JSON file and parse it. I have this code for reading from my file
我正在尝试读取 JSON 文件并解析它。我有这个代码可以从我的文件中读取
StreamReader re = new StreamReader("artists.json");
JsonTextReader reader = new JsonTextReader(re);
But how do I parse it now from reader so I can search data from the file?
但是我现在如何从阅读器解析它以便我可以从文件中搜索数据?
I tried reading the documentationbut couldn't find anything
我尝试阅读文档但找不到任何内容
采纳答案by Serj-Tm
using Newtonsoft.Json;
//..
JsonSerializer se = new JsonSerializer();
object parsedData = se.Deserialize(reader);
回答by Sam
in response to "Some details on how to implement this would be helpful. – aknatn"
回应“有关如何实现这一点的一些细节会有所帮助。 – aknatn”
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
public class Program
{
public static void Main()
{
//JSON = {"Property1":"as","CollectionProperty":[{"prop1":"1","prop2":"2","prop3":"3"}]}
//This Top part is just to build a stream
//- No need to do this if you are accessing a file
string JSON = "{\"Property1\":\"SomePropName\",\"CollectionProperty\":"+
"[{\"prop1\":\"1\",\"prop2\":\"2\",\"prop3\":\"3\"}]}";
byte[] byteArray = Encoding.UTF8.GetBytes(JSON);
//byte[] byteArray = Encoding.ASCII.GetBytes(contents);
MemoryStream stream = new MemoryStream(byteArray);
// convert stream to string
JsonSerializer se = new JsonSerializer();
StreamReader re = new StreamReader(stream);
JsonTextReader reader = new JsonTextReader(re);
var DeserializedObject = se.Deserialize<Collections>(reader);
Console.WriteLine(DeserializeObject.Property1);
//"...so I can search data from the file?"
//This is up to you and how you wanna handle it, but you now have JSON
//Deserialized and stored in memory. 'How to search' depends on objects class
//Also, Original question said he had the JSON. I would recommend using
//json2csharp.com/ or jsonutils.com/
//to retrieve the classes in order to Deserialize it to your object.
//Note 1: You don't always have to cast it
//- I just always try to if and when I can
//Note 2: Because you are using a StreamReader,
//this will account for Large JSON Objects
}
public class Collections
{
public List<CollectionProperty> CollectionProperty = new List<CollectionProperty>();
public string Property1;
}
public class CollectionProperty
{
public string prop1 { get; set; }
public string prop2 { get; set; }
public string prop3 { get; set; }
}
}
回答by andersh
If you want to load it into a JObjector a dynamictype (and not deserializing it into a .NET type), you can use the JObject.Loadmethod
如果要将其加载到JObject或动态类型(而不是将其反序列化为 .NET 类型),则可以使用JObject.Load方法
using(var sr = new StreamReader("artists.json"))
{
var reader = new JsonTextReader(sr);
var jObject = JObject.Load(reader);
//Get property from JObject
var someValue = jObject.GetValue("someProperty").Value<string>();
// JObject can be cast into a dynamic
var dObject = (dynamic)jObject;
someValue = (string)dObject.someProperty;
}

