繁体   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