簡體   English   中英

在C#中從Json文件提取節點

[英]Extracting a node from Json File in C#

在JSON文件中,我想從節點中提取數據。 假設我想提取商品節點內的書節點或值。 這是我的JSON文件。

JSON格式

    {  
   "store":[  
      {  
         "name":"Sunshine Department Store",
         "address":"Wangfujing Street",
         "goods":{  
            "book":[  
               {  
                  "category":"Reference",
                  "title":"Sayings of the Century",
                  "author":"Nigel Rees",
                  "price":8.88
               },
               {  
                  "category":"Fiction",
                  "title":"Sword of Honour",
                  "author":"Evelyn Waugh",
                  "price":12.66
               }
            ],
            "bicycle":{  
               "type":"GIANT OCR2600",
               "color":"White",
               "price":276
            }
         }
      }
   ]
}

private string ParseBookNode(JObject bookJSONFile)
{
    JArray bookJson = null;
    string bookFarmNode = null;
    if (bookJSONFile != null && bookJSONFile["store"] != null)
    {
        bookJson = (JArray)bookJSONFile["store"];
        bookFarmNode = bookJson[0].ToString();

        if (bookJSONFile["book"] != null)
        {
            bookJson = (JArray)bookJSONFile["book"];
            bookFarmNode = bookJson[0].ToString();
        }
    }
    else
    {
        throw new Exception("Book node not found.");
    }
    return bookFarmNode;
}

我如何沿這些路線提取數據?

if (bookJSONFile["book"] != null)
{
    bookJson = (JArray)bookJSONFile["book"];
    bookFarmNode = bookJson[0].ToString();
}

您的代碼與數據結構關系不大,變量名令人困惑,這可能無法幫助您正確組織代碼。

我認為,這(恐怕未經過實驗)應該可以使您訪問book數組(在“ store”數組的第一個對象中)。

private string ParseBookNode(JObject bookJSONFile)
{
    JArray storeList = null;
    JObject store = null;
    JObject goods = null;
    JArray bookList = null;

    if (bookJSONFile != null && bookJSONFile["store"] != null)
    {
        storeList = (JArray)bookJSONFile["store"];
        store = bookJson[0];
        goods = store["goods"];

        if (goods["book"] != null)
        {
            bookList = (JArray)goods["book"];
        }
    }
    else
    {
        throw new Exception("File is empty, or Store node not found.");
    }
    return "something, not sure what you want to return here";
}

對於任何錯誤,我們深表歉意,但希望您能大致了解。 https://www.newtonsoft.com/json/help/html/Introduction.htm包含有關如何使用JArray和JObject的全面文檔。

您可以使用Json.Net訪問它。

我只是添加了按類別查找,目的只是為了向您展示您可以執行以下操作。

    public static JObject GetBook(JObject jObject, string category)
    {
        JObject returnValue = null;
        try
        {
            var array = jObject.Property("store").Value;
            var first = (JObject)array.FirstOrDefault();

            var goods = first?.Property("goods").Value;

            var books = ((JObject)goods).Property("book").Value;

            var booksArray = books as JArray;

            foreach (JObject book in booksArray)
            {
                if (book.Property("category")?.Value?.ToString() == category)
                {
                    returnValue = book;
                    break;
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }

        return returnValue;
    }

您可以嘗試Cinchoo ETL-一個用於解析/編寫JSON文件的開源庫。 這是解析和加載書本節點的方法

using (var jr = new ChoJSONReader("sample9.json").WithJSONPath("$..book")
    )
{
    foreach (var x in jr)
    {
        Console.WriteLine($"Category: {x.category}");
        Console.WriteLine($"Title: {x.title}");
        Console.WriteLine($"Author: {x.author}");
        Console.WriteLine($"Price: {x.price}");
    }
}

如果您的POCO圖書類型定義如下

public class Book
{
    public string Category { get; set; }
    public string Title { get; set; }
    public string Author { get; set; }
    public double Price { get; set; }
}

您可以按以下方式加載它們

using (var jr = new ChoJSONReader<Book>("sample9.json").WithJSONPath("$..book")
)
{
    foreach (var x in jr)
    {
        Console.WriteLine($"Category: {x.Category}");
        Console.WriteLine($"Title: {x.Title}");
        Console.WriteLine($"Author: {x.Author}");
        Console.WriteLine($"Price: {x.Price}");
    }
}

希望這可以幫助。

免責聲明:我是圖書館的作者。

暫無
暫無

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

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