在C#中从Json文件中提取节点

时间:2017-12-27 21:51:08

标签: c# json

在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();
}

3 个答案:

答案 0 :(得分:0)

您的代码与您的数据结构关系不大,而且您的变量名称令人困惑,这可能无法帮助您正确组织代码。

我认为这个(未经考验的我害怕)应该让你访问书籍数组(在"商店"数组的第一个对象中)。

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的全面文档。

答案 1 :(得分:0)

您可以使用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;
    }

答案 2 :(得分:0)

您可以尝试使用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}");
    }
}

希望这有帮助。

免责声明:我是图书馆的作者。