0

I wanted to convert a json into an array but I get "application went to break mode" when I try debugging mode and the program just freezes down if I try to run it.

I used the answer of Convert json to a C# array? but something went wrong.

Could you help me to find the cause of the error?

{
public class MarketHistory
{
    public string Date { get; set; }
    public string Order_Count { get; set; }
    public string Volume { get; set; }
    public string Highest { get; set; }
    public string Avarage { get; set; }
    public string Lowest { get; set; }
}

class Program
{
    public static string DownloadString(string address)
    {
        WebClient client = new WebClient();
        string reply = client.DownloadString(address);

        return reply;
    }

    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");

        string url = "https://esi.tech.ccp.is/latest/markets/10000002/history/?datasource=tranquility&type_id=42";
        var json = DownloadString(url);

        JavaScriptSerializer js = new JavaScriptSerializer();
        MarketHistory[] marketHistories = js.Deserialize<MarketHistory[]>(json);

        Console.ReadKey();
    }
}

}

The json:

 [
  {
    "date": "2016-11-01",
    "order_count": 24,
    "volume": 275,
    "highest": 28.17,
    "average": 28.15,
    "lowest": 28
  },

First fix:

{
public class MarketHistory
{
    public string date { get; set; }
    public string order_count { get; set; }
    public string volume { get; set; }
    public string highest { get; set; }
    public string avarage { get; set; }
    public string lowest { get; set; }
}

class Program
{
    public static string DownloadString(string address)
    {
        WebClient client = new WebClient();
        string reply = client.DownloadString(address);

        return reply;
    }

    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");

        string url = "https://esi.tech.ccp.is/latest/markets/10000002/history/?datasource=tranquility&type_id=42";
        var json = DownloadString(url);

        JavaScriptSerializer js = new JavaScriptSerializer();
        MarketHistory[] marketHistories = js.Deserialize<MarketHistory[]>(json);

        Console.ReadKey();
    }
}

}

I added the reference, any idea why can I get the

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. at Eve_console_app.Program.Main(String[] args)

error?

11
  • Fixed some error. Commented Dec 22, 2017 at 23:01
  • For one, the json string does not capitalize the properties like "order_count", but your class has those capitalized. Order_Count <> order_count. Commented Dec 22, 2017 at 23:09
  • Good catch but there is something else too. I'm getting "break mode" both with f5 and ctrl-f5. Commented Dec 22, 2017 at 23:18
  • I'm getting Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. at Eve_console_app.Program.Main(String[] args) Press any key to continue . . . Error message with ctrl - f5 now. Commented Dec 22, 2017 at 23:25
  • Then I should just add the dll to the folder? Commented Dec 22, 2017 at 23:27

1 Answer 1

2

Use this code and make sure to reference System.Runtime.Serialization dll

using System;
using System.Collections.Generic;
using System.Runtime.Serialization.Json;
namespace StackOverFlow
{
    class Program
    {
        static void Main(string[] args)
        {
            var request = System.Net.WebRequest.Create("https://esi.tech.ccp.is/latest/markets/10000002/history/?datasource=tranquility&type_id=42") as System.Net.HttpWebRequest;
            request.Method = "GET";
            request.ContentLength = 0;
            using (var response = request.GetResponse() as System.Net.HttpWebResponse)
            {
                if (response.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    throw new Exception(response.StatusCode + "\t" + response.StatusDescription);
                }

                DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(List<MarketHistory>));
                var result = jsonSerializer.ReadObject(response.GetResponseStream()) as List<MarketHistory>;
            }
            Console.ReadLine();
        }
    }

    public class MarketHistory
    {
        public string date { get; set; }
        public string order_count { get; set; }
        public string volume { get; set; }
        public string highest { get; set; }
        public string average { get; set; }
        public string lowest { get; set; }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.