簡體   English   中英

用多個對象解析Json newtonsoft

[英]Parse Json with multiple objects newtonsoft

我試圖在C#中使用newtonsoft解析相當復雜/不必要的復雜JSON輸出,但是由於某些原因,我的解析器始終返回null,並且沒有詳細說明這種情況。

我嘗試解析的JSON文件的示例:

   {
    "response": {
        "success": 1,
        "current_time": 1362339098,
        "prices": {
            "35": { 
                "11": { 
                    "0": { 
                        "current": {
                            "currency": "keys",
                            "value": 39,
                            "value_high": 41,
                            "date": 1357515306 
                        },
                        "previous": { 
                            "currency": "keys",
                            "value": 37,
                            "value_high": 39
                        }
                    }
                },
                "3": { 
                    "0": { 
                        "current": {
                            "currency": "metal",
                            "value": 0.33,
                            "value_high": 0.66
                        }
                    }
                }
            },
            "5002": { 
                "6": {
                    "0": {
                        "current": {
                            "currency": "usd",
                            "value": 0.39,
                            "value_high": 0.42,
                            "date": 1358090106
                        }
                    }
                }
            },                          
            "5022": {
                "6": {
                    "1": { 
                        "current": {
                            "currency": "metal",
                            "value": 1.33,
                            "value_high": 1.55,
                            "date": 1357515175
                        }
                    }
                }
            }
        }
    }
}

還有我正在使用的C#解析器。 我運行getCurrentPrices()返回PriceParser對象,但返回的對象始終為null。

using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using System.Diagnostics;

namespace SteamBot
{
    class PriceParser
    {
        //Methods
        public PriceParser updatePrices()
        {
            var json = File.ReadAllText("test.json");
            ParserResult result = JsonConvert.DeserializeObject<ParserResult>(json);
            return result.result;
        }

        public Data currentPrices { get; set; }

        //DATA
        public class Data
        {
            public Response Response { get; set; }
        }

        public class Response 
        {
           public string success { get; set; }
           public string current_time {get; set;}
          public List<Price> prices { get; set;}
        }

        public class Price
        {
            public int defindex { get; set; }
            public int quality { get; set; }
            public Current current { get; set; }
            public Previous previous { get; set; }
        }

        public class Current
        {
            public string currency { get; set; }
            public float value { get; set; }
            public float value_high { get; set; }
            public int date { get; set; }
        }

        public class Previous
        {
            public string currency { get; set; }
            public float value { get; set; }
            public float value_high { get; set; }
            public int date { get; set; }
        }

        protected class ParserResult
        {
            public PriceParser result { get; set; }
        }
    }
}

我可能只是想念一些愚蠢的東西,但是對於我自己的一生,我無法弄清楚是什么,任何具有更多JSON爭吵經驗的人都知道這是怎么回事?

由於類結構與JSON不匹配,您將獲得空值。

第一個問題是,當您應該使用Data時,您正在反序列化為ParserResult Data具有一個response屬性,與您的JSON相匹配。 ParserResult沒有此屬性。

第二個問題是,您已將prices定義為List<Price> ,但是您的JSON不包含數組。 相反,JSON結構實際上是一系列嵌套的字典。

嘗試像這樣定義內部類:

public class Data
{
    public Response response { get; set; }
}

public class Response
{
    public int success { get; set; }
    public long current_time { get; set; }
    public IDictionary<int, IDictionary<int, IDictionary<int, Price>>> prices { get; set; }
}

public class Price
{
    public Quote current { get; set; }
    public Quote previous { get; set; }
}

public class Quote
{
    public string currency { get; set; }
    public decimal value { get; set; }
    public decimal value_high { get; set; }
    public long date { get; set; }
}

然后,在您的updatePrices方法中,您可以像這樣反序列化:

public PriceParser updatePrices()
{
    var json = File.ReadAllText("test.json");
    currentPrices = JsonConvert.DeserializeObject<Data>(json);
    return this;
}

這是轉儲數據的方式:

PriceParser parser = new PriceParser();
parser.updatePrices();

foreach (var defindex in parser.currentPrices.response.prices)
{
    Console.WriteLine("defindex: " + defindex.Key);
    foreach (var quality in defindex.Value)
    {
        Console.WriteLine("\t quality: " + quality.Key);
        foreach (var price in quality.Value)
        {
            Console.WriteLine("\t\t index: " + price.Key);
            Console.WriteLine("\t\t\t current price:");
            Console.WriteLine("\t\t\t\t currency: " + price.Value.current.currency);
            Console.WriteLine("\t\t\t\t value: " + price.Value.current.value);
            Console.WriteLine("\t\t\t\t value_high: " + price.Value.current.value_high);
            if (price.Value.previous != null)
            {
                Console.WriteLine();
                Console.WriteLine("\t\t\t previous price:");
                Console.WriteLine("\t\t\t\t currency: " + price.Value.previous.currency);
                Console.WriteLine("\t\t\t\t value: " + price.Value.previous.value);
                Console.WriteLine("\t\t\t\t value_high: " + price.Value.previous.value_high);
            }
        }
    }
}

這是上面的輸出:

defindex: 35
         quality: 3
                 index: 0
                         current price:
                                 currency: metal
                                 value: 0.33
                                 value_high: 0.66
         quality: 11
                 index: 0
                         current price:
                                 currency: keys
                                 value: 39
                                 value_high: 41

                         previous price:
                                 currency: keys
                                 value: 37
                                 value_high: 39
defindex: 5002
         quality: 6
                 index: 0
                         current price:
                                 currency: usd
                                 value: 0.39
                                 value_high: 0.42
defindex: 5022
         quality: 6
                 index: 1
                         current price:
                                 currency: metal
                                 value: 1.33
                                 value_high: 1.55

暫無
暫無

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

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