从网站获取Json,反序列化并输出到控制台(输出什么?)

时间:2016-09-13 10:43:18

标签: c# json deserialization

问题是,代码是没有问题的compilig,但我希望在控制台中反序列化Json文件而不是:

image

我的代码是:

using System;
using System.IO;
using System.Net;
using Newtonsoft.Json;
using System.Web.Script.Serialization;
using System.Collections.Generic;

namespace GetRequest
{
    class PARSE
    {
        public int userID { get; set; }
        public int id { get; set; }
        public string title { get; set; }
        public string body { get; set; }

        public override string ToString()
        {
            return string.Format("UserID: {0} \nid: {1} \ntitle: {2} \nbody {3}", userID, id, title, body);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {

            // Create the web request  
            HttpWebRequest request = WebRequest.Create("https://jsonplaceholder.typicode.com/posts") as HttpWebRequest;

            // Get response  
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)

            {
                // Get the response stream  
                StreamReader reader = new StreamReader(response.GetResponseStream());

                {
                    string myString = reader.ReadToEnd();
                    System.IO.File.WriteAllText(@"C:\Users\admin\Documents\Visual Studio 2015\Projects\WriteText.json", myString);
                }


                // JSON deserialize from a file
                String JSONstring = File.ReadAllText(@"C:\Users\admin\Documents\Visual Studio 2015\Projects\WriteText.json");
                List<PARSE> p = JsonConvert.DeserializeObject<List<PARSE>>(JSONstring);           
                Console.WriteLine(p);
                Console.ReadLine();
                
            }
        }
    }
}

如果有人可以帮我一点,我将感激不尽:)

1 个答案:

答案 0 :(得分:1)

您正在打印列表,使用循环迭代对象列表然后将其打印出来。

试试这个:

using System;
using System.IO;
using System.Net;
using Newtonsoft.Json;
using System.Web.Script.Serialization;
using System.Collections.Generic;

namespace GetRequest
{
    class PARSE
    {
        public int userID { get; set; }
        public int id { get; set; }
        public string title { get; set; }
        public string body { get; set; }

        public override string ToString()
        {
            return string.Format("UserID: {0} \nid: {1} \ntitle: {2} \nbody {3}", userID, id, title, body);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // Create the web request  
            HttpWebRequest request = WebRequest.Create("https://jsonplaceholder.typicode.com/posts") as HttpWebRequest;

            // Get response  
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                // Get the response stream  
                StreamReader reader = new StreamReader(response.GetResponseStream());
                {
                    string myString = reader.ReadToEnd();
                    System.IO.File.WriteAllText(@"C:\Users\admin\Documents\Visual Studio 2015\Projects\WriteText.json", myString);
                }
                // JSON deserialize from a file
                String JSONstring = File.ReadAllText(@"C:\Users\admin\Documents\Visual Studio 2015\Projects\WriteText.json");
                List<PARSE> pList = JsonConvert.DeserializeObject<List<PARSE>>(JSONstring);           
                foreach(PARSE p in pList) {
                    Console.WriteLine(p);
                    Console.ReadLine();
                }
            }
        }
    }
}