Hegedus.Cs Hegedus.Cs - 11 months ago 110
JSON Question

c# json to array debugging "application went to break mode"

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?

Answer Source

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; }
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download