如何将此JSON字符串解析为2 List< String> [英] How to parse this JSON string into 2 List<String>

查看:110
本文介绍了如何将此JSON字符串解析为2 List< String>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some problem to understand how you parse this JSON string. As seen we have 2 lists in the JSON string. "bids" and "asks"

例如,对于出价,我们有:

0.035314,25.986

0.035313,6.947


For bids for example we have:
0.035314,25.986
0.035313,6.947
etc

目标是创建2个列表,出价并询问每个元素在哪里列表中包含以上内容。例如,每个索引包含以下信息: 0.035314,25.986等。

执行此操作时如何处理此字符串?

The goals is to create 2 lists, bids and asks where each element in the list contains the above. For example each index contains this information then: "0.035314,25.986" etc.
How will one approach this string when doing this?

期望输出应该像下面这样理解:

Expected output should be as an understanding like below:

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

JSON is located here: 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; }
}


推荐答案

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

You need an intermediate class to reflect the JSON structure:

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

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

Then you can parse the JSON and convert to the desired structure:

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

这篇关于如何将此JSON字符串解析为2 List&lt; String&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆