如何将此JSON字符串解析为2 List <string>

时间:2019-03-05 19:51:01

标签: c# json deserialization

我在理解如何解析此JSON字符串时遇到了一些问题。 可以看到,JSON字符串中有2个列表。 “出价”和“询问”

例如,对于出价,我们有:
0.035314,25.986
0.035313,6.947
等等

目标是创建2个列表,出价并询问列表中每个元素在何处包含以上内容。例如,每个索引都包含以下信息:“ 0.035314,25.986”等。
这样做时,将如何处理此字符串?

预期输出应如下所示:

List<String> bidsLIST = new List<String>();
List<String> asksLIST = new List<String>();
bidsLIST.Add("0.035314,25.986");
bidsLIST.Add("0.035313,6.947");

asksLIST .Add("0.035319,1.139");
asksLIST .Add("0.03532,28.381");

JSON位于此处: https://pastebin.com/j6Xckh49

{"bids":[[0.035314,25.986],[0.035313,6.947],[0.035312,17.441],[0.035308,4],[0.0353,6.188]],"asks":[[0.035319,1.139],[0.03532,28.381],[0.035324,6.7],[0.035329,2.307],[0.03533,6.868]],"nonce":451939443}

此代码并不完全正确:

using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
void testparseJSON()
{
    String responseBody = "{" +
                            '"' + "bids" + '"' + ":[[0.035314,25.986],[0.035313,6.947],[0.035312,17.441],[0.035308,4],[0.0353,6.188]]," +
                            '"' + "asks" + '"' + ":[[0.035319,1.139],[0.03532,28.381],[0.035324,6.7],[0.035329,2.307],[0.03533,6.868]]," +
                            '"' + "nonce" + '"' + ":451939443}";
    var deserializedTickers = JsonConvert.DeserializeObject<Dictionary<List<String>, bidsasks>>(responseBody);
    foreach (var bidsasks in deserializedTickers)
    {
        var Bids = bidsasks.Value.bids;
        var Asks = bidsasks.Value.asks;
        if (Bids != null && Asks != null)
        {
            //How to get the 2 lists here?
        }
    }
}
public class bidsasks
{
    public List<String> bids { get; set; }
    public List<String> asks { get; set; }
}

4 个答案:

答案 0 :(得分:1)

    void testparseJSON(){

 String responseBody = "{" +
                                        '"' + "bids" + '"' + ":[[0.035314,25.986],[0.035313,6.947],[0.035312,17.441],[0.035308,4],[0.0353,6.188]]," +
                                        '"' + "asks" + '"' + ":[[0.035319,1.139],[0.03532,28.381],[0.035324,6.7],[0.035329,2.307],[0.03533,6.868]]," +
                                        '"' + "nonce" + '"' + ":451939443}";

                var jsnAsObj = Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject>(responseBody);
                var bids = jsnAsObj.bids;
                var asks = jsnAsObj.asks;
}

并将您的DM放在另一个类(建议在新的* .cs文件中)。 这只是一个建议,而不是必须的,该代码可以作为当前类的内部类工作,但不建议这样做。

public class Rootobject
    {
        public float[][] bids { get; set; }
        public float[][] asks { get; set; }
        public int nonce { get; set; }
    }

答案 1 :(得分:1)

您需要一个中间类来反映JSON结构:

public class JsonBidsAsks {
    public List<List<string>> bids { get; set; }
    public List<List<string>> asks { get; set; }
}

然后,您可以解析JSON并转换为所需的结构:

var deserializedTicker = JsonConvert.DeserializeObject<JsonBidsAsks>(responseBody);
var ans = new bidsasks {
    bids = deserializedTicker.bids.Select(ba => ba.Join(",")).ToList(),
    asks = deserializedTicker.asks.Select(aa => aa.Join(",")).ToList(),
};

答案 2 :(得分:0)

您可以将json反序列化为匿名类型:

var template = new
{
    bids = new[]
    {
        new[] {1.0, 1.0}
    },
    asks = new[]
    {
        new[] {1.0, 1.0}
    },
};

var parsedJson = JsonConvert.DeserializeAnonymousType(responseBody, template);

现在只需在两个列表中加入bidsasks

var bidsLIST = parsedJson.bids.Select(b => string.Join(",", b)).ToList();
var asksLIST = parsedJson.asks.Select(a => string.Join(",", a)).ToList();   

答案 3 :(得分:0)

检查@NetMage 的答案后,我对代码进行了一些操作,并且没有问题。

public class bidsasks
{
    public List<List<string>> bids { get; set; }
    public List<List<string>> asks { get; set; }
    public int nonce { get; set; }

}
public static void testparseJSON()
{
    String responseBody = "{" +
                    '"' + "bids" + '"' + ":[[0.035314,25.986],[0.035313,6.947],[0.035312,17.441],[0.035308,4],[0.0353,6.188]]," +
                    '"' + "asks" + '"' + ":[[0.035319,1.139],[0.03532,28.381],[0.035324,6.7],[0.035329,2.307],[0.03533,6.868]]," +
                    '"' + "nonce" + '"' + ":451939443}";
        var deserializedTicker = JsonConvert.DeserializeObject<bidsasks>(responseBody);
        var ans = new bidsasks
        {
            bids = deserializedTicker.bids,
            asks = deserializedTicker.asks
        };
        for (int i = 0; i < ans.bids.Count; i++)
        {
            Console.WriteLine(String.Format("PRICE", ans.bids[i][0]));
            Console.WriteLine(String.Format("QTY", ans.bids[i][1]));
        }
        for (int i = 0; i < ans.asks.Count; i++)
        {
            Console.WriteLine(String.Format("PRICE", ans.asks[i][0]));
            Console.WriteLine(String.Format("QTY", ans.asks[i][1]));
        }
    }
}