【问题标题】:c# json to array debugging "application went to break mode"c# json to array 调试“应用程序进入中断模式”
【发布时间】:2018-06-05 12:09:24
【问题描述】:

我想将一个 json 转换为一个数组,但是当我尝试调试模式时出现“应用程序进入中断模式”,如果我尝试运行它,程序就会冻结。

我使用的答案是 Convert json to a C# array? 但出了点问题。

您能帮我找出错误的原因吗?

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

}

json:

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

第一次修复:

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

}

我添加了参考,知道为什么我可以得到

未处理的异常:System.IO.FileNotFoundException:无法加载文件或程序集“System.Web.Extensions,版本=4.0.0.0,文化=中性,PublicKeyToken=31bf3856ad364e35”。该系统找不到指定的文件。 在 Eve_console_app.Program.Main(String[] args)

错误?

【问题讨论】:

  • 修复了一些错误。
  • 一方面,json 字符串不会将“order_count”之类的属性大写,但您的类将这些属性大写。 Order_Count order_count。
  • 不错,但还有其他东西。我正在使用 f5 和 ctrl-f5 获得“中断模式”。
  • 我收到未处理的异常:System.IO.FileNotFoundException:无法加载文件或程序集“System.Web.Extensions,版本=4.0.0.0,文化=中性,PublicKeyToken=31bf3856ad364e35”。该系统找不到指定的文件。在 Eve_console_app.Program.Main(String[] args) 按任意键继续。 . .现在使用 ctrl - f5 出现错误消息。
  • 那我应该把 dll 添加到文件夹吗?

标签: c# arrays json


【解决方案1】:

使用此代码并确保引用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; }
    }
}

【讨论】:

    猜你喜欢
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2017-10-31
    • 1970-01-01
    相关资源
    最近更新 更多