简体   繁体   中英

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. It should be simple but I get the following error message:

Unexpected character encountered while parsing value: Q. Path '', line 0, position 0.

My json string is the following:

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.

I used the following website:

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:

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

for further reference look at their C# api docs: https://github.com/HubertJ/QuandlCS

The string is indeed a json string. Thanks for the link you provided. I made a silly mistake. Quandl was returning the link to the string. So I used HttpWebRequest to obtain the string itself. I then edited the deserialization as you suggest. I think it worked. The problem now is that it is not binding to my 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.

Second:

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

you are trying to deserialize an array of RootObject. but the provided json only contains one. 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/ ). 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. but List<List<object>> is worth a try. if it gives an error try List<List<string>> instead. if that doesnt work you will probably have to do it manually.

Check the json with a debugger please . i think what you are trying to deserialize is NOT what you posted here!

also: from what library do you use this: JsonConvert.DeserializeObject ?

Fifth:

who the **** designed their api to produce that kind of json:

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

that is just plain and simple retarded (i just had to say it)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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