How to parse this JSON string into 2 List<String>

回眸只為那壹抹淺笑 提交于 2019-12-11 17:19:18

问题


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"

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

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

This code is not entirely correct:

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

回答1:


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

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



回答2:


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

and put at another class (recommended at a new *.cs file) your DM. this is just a suggestion and not a must, the code can work as an inner class inside of your current one, but it's not recommended.

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



回答3:


You can deserialize your json to anonymous type:

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

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

Now just join bids and asks in two lists:

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


来源:https://stackoverflow.com/questions/55010495/how-to-parse-this-json-string-into-2-liststring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!