简体   繁体   English

来自Quandl的C#反序列化Json字符串

[英]C# Deserialize Json string from Quandl

I have spent a few hours on this and still can't find the answer. 我已经花了几个小时,但仍然找不到答案。 I have a Json string I need to deserialize. 我有一个需要反序列化的Json字符串。 It should be simple but I get the following error message: 它应该很简单,但是我收到以下错误消息:

Unexpected character encountered while parsing value: Q. Path '', line 0, position 0. 解析值时遇到意外字符:Q.路径”,第0行,位置0。

My json string is the following: 我的json字符串如下:

https://www.quandl.com/api/v1/datasets/FRED/GDP.json?auth_token=Mi1xP1q2776TU4kmGcHo&collapse=monthly&transformation=none&sort_order=asc&rows=100 https://www.quandl.com/api/v1/datasets/FRED/GDP.json?auth_token=Mi1xP1q2776TU4kmGcHo&collapse=monthly&transformation=none&sort_order=asc&rows=100

It is a link to a Json string provided by the Quandl online database. 它是Quandl在线数据库提供的Json字符串的链接。

I used the following website: 我使用了以下网站:

http://json2csharp.com/ http://json2csharp.com/

to generate the necessary class. 生成必要的类。

I understand I then to have to deserialize into this class using: 我知道自己必须使用以下方法反序列化此类:

var result = JsonConvert.DeserializeObject<List<RootObject>>(request.jsonString);

but it is not working. 但它不起作用。

Here is my full code: 这是我的完整代码:

    public class Errors
    {
    }

    public class RootObject
    {
        public Errors errors { get; set; }
        public int id { get; set; }
        public string source_name { get; set; }
        public string source_code { get; set; }
        public string code { get; set; }
        public string name { get; set; }
        public string urlize_name { get; set; }
        public string display_url { get; set; }
        public string description { get; set; }
        public string updated_at { get; set; }
        public string frequency { get; set; }
        public string from_date { get; set; }
        public string to_date { get; set; }
        public List<string> column_names { get; set; }
        public bool @private { get; set; }
        public object type { get; set; }
        public bool premium { get; set; }
        public List<List<object>> data { get; set; }
    }
    private void PullFromQuandl()
    {
        QuandlDownloadRequest request = new QuandlDownloadRequest();
        request.APIKey = "Mi1xP1q2776TU4kmGcHo";
        request.Datacode = new Datacode("FRED", "GDP");
        request.Format = FileFormats.JSON;
        request.Frequency = Frequencies.Monthly;
        request.Truncation = 100;
        request.Sort = SortOrders.Ascending;

        string jsonString = request.ToRequestString();

        var result = JsonConvert.DeserializeObject<List<RootObject>> (jsonString);
        }

You could mark this as an answer, because what you posted as an answer could be done much more easily from a look at the quandl api: 您可以将其标记为答案,因为通过查看quandl api,可以更轻松地完成发布为答案的内容:

using QuandlCS.Connection; // need to use this

and then 接着

QuandlConnection conn = new QuandlConnection ();
string json = conn.Request(request); // request is your QuandlDownloadRequst
RootObject obj = JsonConvert.DeserializeObject<RootObject>(json);

that is the same as in you answer. 那和你回答中的一样。 they have a class for that :D 他们有一堂课:D

for further reference look at their C# api docs: https://github.com/HubertJ/QuandlCS 有关更多参考,请查看他们的C#API文档: https : //github.com/HubertJ/QuandlCS

The string is indeed a json string. 该字符串确实是json字符串。 Thanks for the link you provided. 感谢您提供的链接。 I made a silly mistake. 我犯了一个愚蠢的错误。 Quandl was returning the link to the string. Quandl正在将链接返回到字符串。 So I used HttpWebRequest to obtain the string itself. 因此,我使用HttpWebRequest来获取字符串本身。 I then edited the deserialization as you suggest. 然后,我按照您的建议编辑了反序列化。 I think it worked. 我认为它有效。 The problem now is that it is not binding to my datagridview. 现在的问题是它没有绑定到我的datagridview。

        private void PullFromQuandl()
       {
        QuandlDownloadRequest request = new QuandlDownloadRequest();
        request.APIKey = "Mi1xP1q2776TU4kmGcHo";
        request.Datacode = new Datacode("CURRFX", "EURUSD");
        request.Format = FileFormats.JSON;
        request.Frequency = Frequencies.Monthly;
        request.Truncation = 100;
        request.Sort = SortOrders.Ascending;
        string jsonString = request.ToRequestString();

        HttpWebRequest request2 = null;
        HttpWebResponse response = null;
        string returnData = string.Empty;
        request2 = (HttpWebRequest)System.Net.WebRequest.Create(new Uri(jsonString));
        request2.Method = "GET";
        response = (HttpWebResponse)request2.GetResponse();

        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        returnData = reader.ReadToEnd();
        var result = JsonConvert.DeserializeObject<RootObject>(returnData);
        dgvPending.DataSource = result;
        }

First of all: Avoid object type. 首先:避免对象类型。 JSON Serializers -> Deserializers usually have problems with it. JSON序列化程序->反序列化程序通常会遇到问题。

Second: 第二:

JsonConvert.DeserializeObject<List<RootObject>> (jsonString);

you are trying to deserialize an array of RootObject. 您正在尝试反序列化RootObject数组。 but the provided json only contains one. 但提供的json只包含一个。 it should be: 它应该是:

JsonConvert.DeserializeObject<RootObject> (jsonString);

Third: 第三:

string jsonString = request.ToRequestString();

can you debug and look if jsonString actually contains valid json (use http://jsonlint.org/ ). 您可以调试并查看jsonString实际包含有效的json(使用http://jsonlint.org/ )。 Seems to me you would need a response string and not a request string. 在我看来,您将需要一个响应字符串,而不是一个请求字符串。 (I think that is why you get the error!!!) (我认为这就是您得到错误的原因!!!)

Fourth: 第四:

there will probably be more errors. 可能会有更多错误。 I kinda doubt that a json deserializer with default settings will be able to decode the data array properly. 我有点怀疑具有默认设置的json解串器是否能够正确解码数据数组。 but List<List<object>> is worth a try. 但是List<List<object>>值得一试。 if it gives an error try List<List<string>> instead. 如果给出错误,请尝试使用List<List<string>> if that doesnt work you will probably have to do it manually. 如果那不起作用,您可能必须手动进行。

Check the json with a debugger please . 请使用调试器检查json i think what you are trying to deserialize is NOT what you posted here! 我认为您要反序列化的不是您在此处发布的内容!

also: from what library do you use this: JsonConvert.DeserializeObject ? 还:您使用哪个库: JsonConvert.DeserializeObject

Fifth: 第五:

who the **** designed their api to produce that kind of json: ****设计了自己的api来生成这种json的人:

[
        [
            "1947-01-31",
            243.1
        ]
]

that is just plain and simple retarded (i just had to say it) 那只是普通而简单的弱智(我不得不说)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM