簡體   English   中英

如何使用JSON.net解析JSON文件

[英]How do you parse a JSON file using JSON.net

我試圖讀取一個JSON文件並解析它。 我有這個代碼從我的文件中讀取

StreamReader re = new StreamReader("artists.json");
JsonTextReader reader = new JsonTextReader(re);

但是我現在如何從讀者解析它以便我可以從文件中搜索數據?

我試過閱讀文檔但找不到任何東西

    using Newtonsoft.Json;

    //..

    JsonSerializer se = new JsonSerializer();
    object parsedData = se.Deserialize(reader);

回應“有關如何實施這一點的一些細節會有所幫助。 - 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; }
    }
  }

如果要將其加載到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;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM