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

问题描述 投票:0回答:3

我有一些问题要理解你如何解析这个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; }
}
c# json deserialization
3个回答
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(),
};

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; }
    }

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();   
© www.soinside.com 2019 - 2024. All rights reserved.