【问题标题】:How JSON can be used and implemented in .net? [closed]如何在 .net 中使用和实现 JSON? [关闭]
【发布时间】:2013-01-17 10:09:51
【问题描述】:

谁能帮我看看如何在 ASP.Net 中使用 JSON?我不知道 AJAX 和 JQuery。我只是一个初学者。 (是否可以在 ASP.Net 和 Console Application 中实现?)

【问题讨论】:

标签: asp.net json json.net


【解决方案1】:
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("(link for fetching json data)");
    myReq.Method = WebRequestMethods.Http.Get;
    myReq.Accept = "apllication/json";
    var response = (HttpWebResponse)myReq.GetResponse();
    string myString= response.GetResponseHeader("Registration_Status");
    using (var sr = new StreamReader(response.GetResponseStream()))
    {
        json = sr.ReadToEnd();

        StreamWriter sw = new StreamWriter(@"D:\others\DemoCreatingCustomEvents\TextFile1.txt");
        sw.Write(label1.Content.ToString());
        sw.Close();
    }
}

string json="";

private void button1_Click(object sender, RoutedEventArgs e)
{

    ProductCategory deserializedProdCategory = new ProductCategory();
    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
    DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedProdCategory.GetType());

    deserializedProdCategory = ser.ReadObject(ms) as ProductCategory;
    ms.Close();


//you can fetch the data here and it is shown in label
        label1.Content = deserializedProdCategory.ProdCategory[1].Product_ID + "//" + deserializedProdCategory.ProdCategory[1].Product_Name;
    }
    public class ProductCategory
    {
        public List<Product> ProdCategory = new List<Product>();
        public ProductCategory()
        {
            ProdCategory = new List<Product>();
        }
    }


    public class Product
    {
        public int Product_ID { get; set; }
        public string Product_Code { get; set; }
        public string Product_Name { get; set; }
        public Uri Image_Small { get; set; }
    }

【讨论】:

    猜你喜欢
    • 2010-11-27
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多