如何将json中获取的值传递给字符串

如何将json中获取的值传递给字符串

本文介绍了如何将json中获取的值传递给字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过json获取数据。我想在字符串中收集这些数据,然后插入到

sql表中。我使用下面的代码,它工作正常,但我想知道如何在字符串中收集它们以插入表格。例如,我想收集字符串的countryname,regionname和cityname。我的代码:



I get data through json. I would like to collect those datas in strings and then insert into a
sql table. I used the following code which works fine but I would like to know how to collect them in string for insertion into tables. For instance I would like to collect the countryname,regionname and cityname to the strings. My code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Net;
using System.Web.Script.Serialization;
using System.Web.UI.WebControls;
public partial class getipaddress_test : System.Web.UI.Page
{
    public class Location
    {
        public string IPAddress { get; set; }
        public string CountryName { get; set; }
        public string CountryCode { get; set; }
        public string CityName { get; set; }
        public string RegionName { get; set; }
        public string ZipCode { get; set; }
        public string Latitude { get; set; }
        public string Longitude { get; set; }
        public string TimeZone { get; set; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (string.IsNullOrEmpty(ipAddress))
        {
            ipAddress = Request.ServerVariables["REMOTE_ADDR"];
        }

        string APIKey = "<your api="" key="">";
        string url = string.Format("http://api.ipinfodb.com/v3/ip-city/?key=7d0f9f121cca47d87c9ad6da04748bcd04a43387b21960902b6c9b6d4a84b34z&ip={1}&format=json", APIKey, ipAddress);
        using (WebClient client = new WebClient())
        {
            string json = client.DownloadString(url);
            //string v_cn = ("cityname");
            Location location = new JavaScriptSerializer().Deserialize<location>(json);
            List<location> locations = new List<location>();
            locations.Add(location);
            gvLocation.DataSource = locations;
            gvLocation.DataBind();
        }
    }
}





谢谢。



Thanks.

推荐答案


这篇关于如何将json中获取的值传递给字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 23:44