您好,欢迎来到二三娱乐。
搜索
您的当前位置:首页Read JSON data from URL in ASP.N

Read JSON data from URL in ASP.N

来源:二三娱乐

we have an URL which contains the JSON data, now I am trying to use to read it.

step1: set up

  1. create a website in visual studio and right click the Solution Explorer panel on the left, select Manage NuGet Package...
  2. notice to switch Package source to All or nuget.org and then Browse "Newtonsoft.Json" and install
    Ref:

step2: create model

FoodNearbyModel.cs

public class FoodNearbyModel
{
    public string API_Status { get; set; }
    public string AdminRemarks { get; set; }
    public string Category { get; set; }
    public string ContactEmail { get; set; }
    public string ContactInfo { get; set; }
    public string ContactNumber { get; set; }
    public string ContactStatus { get; set; }
    public float Distance { get; set; }
    public string EatBookArticle { get; set; }
    public string EntryAddress { get; set; }
    public string EntryDescription { get; set; }
    public int EntryID { get; set; }
    public string EntryName { get; set; }
    public string FacebookPage { get; set; }
    public string Instagram { get; set; }
    public float Latitude { get; set; }
    public float Longitude { get; set; }
    public string MasterCategory { get; set; }
    public string OpeningHours { get; set; }
    public string PostalCode { get; set; }
    public string SubFilter { get; set; }
    public string TSLArticle { get; set; }
    public string TSLReviews { get; set; }
    public string Website { get; set; }
    public string Zone { get; set; }
}

step3: create JSonParser

add another .cs file name as JSonParser

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using 
using Newtonsoft.Json;

public class JSonParser
{
    const string url = 
    static List<FoodNearbyModel> foodNearby;
    
    //public JSonParser()
    //{
    //    string json = new WebClient().DownloadString(url);
    //    foodNearby = JsonConvert.DeserializeObject<List<FoodNearbyModel>>(json);
    //}

    public static void initialise()
    {
        string json = new WebClient().DownloadString(url);
        foodNearby = JsonConvert.DeserializeObject<List<FoodNearbyModel>>(json);
    }
    public static List<FoodNearbyModel>GetAll()
    {
        initialise();
        return foodNearby;
    }
    public static List<string> GetByCategory()
    {
        initialise();
        return foodNearby.Select(x => x.Category).Distinct().ToList();
    }
}

step4: call JSonParse in webForm

using System;

public partial class DefaultPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataSource = JSonParser.GetAll();
        GridView1.DataBind();
    }
}

Copyright © 2019- yule263.com 版权所有 湘ICP备2023023988号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务