简体   繁体   English

从文件读取Json数据

[英]Read Json data from file

Hello i'm trying to read json data from a text file but it doesn't work. 您好,我正在尝试从文本文件读取json数据,但是它不起作用。 when i try to get data from a website then it works fine. 当我尝试从网站获取数据时,它可以正常工作。 Anyone know a solution to fix this problem ? 有人知道解决此问题的解决方案吗? thanks in advance. 提前致谢。

            private void Form1_Load(object sender, EventArgs e)
        {
            string path = @"C:\Users\spacefrog\Documents\Visual Studio 2017\Projects\json_test_solution\json_test\bin\Debug\test.json";

          //  Stream stream = File.OpenRead(path);
            WebClient client = new WebClient();
            Stream stream = client.OpenRead("https://www.cryptocompare.com/api/data/coinlist/"); // works perfect
            /*
            Stream stream = client.OpenRead(path); // doesn't work
            Stream stream = File.OpenRead(path); // doesn't work
            */

/* File Data content (same as website)
{
"Response": "Success",
"Message": "Coin list succesfully returned!",
"BaseImageUrl": "https://www.cryptocompare.com",
"BaseLinkUrl": "https://www.cryptocompare.com",
"Data": {
"LTC": {
"Id": "3808",
"Url": "/coins/ltc/overview",
"ImageUrl": "/media/19782/ltc.png",
"Name": "LTC",
"CoinName": "Litecoin",
"FullName": "Litecoin (LTC)",
"Algorithm": "Scrypt",
"ProofType": "PoW",
"SortOrder": "2"
}
...
},
"Type": 100
}
    */

StreamReader reader = new StreamReader(stream);
JObject json = JObject.Parse(reader.ReadLine());
stream.Close();



MessageBox.Show((string)json["Response"]);

}
JObject json = JObject.Parse(reader.ReadLine());

How should the JSON.NET Parser parse the first line? JSON.NET解析器应如何解析第一行? Looking at your comment, the first line seems to be 看您的评论,第一行似乎是

{

Does not look like a valid JSON object. 看起来不像是有效的JSON对象。 Take a look at the ReadToEnd method. 看一下ReadToEnd方法。 By the way, you may want to use the StreamReader within a using scope. 顺便说一句,您可能想在使用范围内使用StreamReader。

You don't have to use a temp file to parse that json 您不必使用临时文件来解析该json

WebClient webClient = new WebClient();
var json = webClient.DownloadString("https://www.cryptocompare.com/api/data/coinlist/");

var rootObj = JsonConvert.DeserializeObject<SOTest.RootObject>(json);
//OR var jObj = JObject.Parse(json);

if you really want to save to a file you can use 如果您真的要保存到文件,可以使用

webClient.DownloadFile(url,filename);

or write the json above to a file 或将上面的json写入文件

File.WriteAllText(filename, json);

or if you want to go the client.OpenRead way 或者如果你想去client.OpenRead方式

var f = File.Create(filename);
stream.CopyTo(f);

public class SOTest
{
    public class DataItem
    {
        public string Id { get; set; }
        public string Url { get; set; }
        public string ImageUrl { get; set; }
        public string Name { get; set; }
        public string Symbol { get; set; }
        public string CoinName { get; set; }
        public string FullName { get; set; }
        public string Algorithm { get; set; }
        public string ProofType { get; set; }
        public string FullyPremined { get; set; }
        public string TotalCoinSupply { get; set; }
        public string PreMinedValue { get; set; }
        public string TotalCoinsFreeFloat { get; set; }
        public string SortOrder { get; set; }
    }

    public class RootObject
    {
        public string Response { get; set; }
        public string Message { get; set; }
        public string BaseImageUrl { get; set; }
        public string BaseLinkUrl { get; set; }
        public Dictionary<string,DataItem> Data { get; set; }
        public int Type { get; set; }
    }
}

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

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