C# ASP.NET - How to Modify a JSON object from API and then display

时间:2017-08-13 13:43:24

标签: c# asp.net json

So I have a web application and I am trying to modify a value from the JSON object and then to display it.

However currently, once I have modified (formatted the time) and attempt to display it, the old value is being displayed - i.e the value from the JSON object.

Can't seem to figure out how to 'save' my changes.

My .apsx code is:

    <asp:Repeater ID="Repeater_weatherReports" runat="server" onitemcommand="reptrData_ItemCommand">
        <ItemTemplate>
            <table id="tblWeather" border="0" visible="true">
                   <td>
                        **date:<asp:Label runat="server" ID="DateWeather" Text='<%# Eval("dt") %>' />&nbsp;**
                    </td>
                </tr>
            </table>
        </ItemTemplate>
    </asp:Repeater>

My C# code :(simplified)

public partial class _Default : System.Web.UI.Page
{

    //string city_name;
    string temp_min;
    string temp_max;
    string humidity;
    DateTime weatherdate;



    protected void reptrData_ItemCommand(object source, RepeaterCommandEventArgs e)
    {


        Label LblDate = e.Item.FindControl("DateWeather") as Label;
       // weatherdate = LblDate.Text;


    }



    protected void GetWeatherInfo(object sender, EventArgs e)
    {




        using (WebClient client = new WebClient())
        {
            string json = client.DownloadString(url);

            JavaScriptSerializer serializer = new JavaScriptSerializer();

            WeatherInfo weatherinfo = serializer.Deserialize<WeatherInfo>(json);

            Repeater_weatherReports.DataSource = weatherinfo.list;
            Repeater_weatherReports.DataBind();





            foreach (List list in weatherinfo.list)
            {


                if (list != null)

                {

                    //var weatherdate = TimeSpan.FromSeconds(list.dt);

                    **var weatherdate = (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddSeconds(list.dt);**




                    temp_min = string.Format("{0}", Math.Round(list.temp.min, 1));
                    temp_max = string.Format("{0}", Math.Round(list.temp.max, 1));
                    humidity = list.humidity.ToString();

                }




            }





        }
    }


    public class Coord
    {
        public double lon { get; set; }
        public double lat { get; set; }
    }

    public class City
    {
        public int id { get; set; }
        public string name { get; set; }
        public Coord coord { get; set; }
        public string country { get; set; }
        public int population { get; set; }
    }

    public class Temp
    {
        public double day { get; set; }
        public double min { get; set; }
        public double max { get; set; }
        public double night { get; set; }
        public double eve { get; set; }
        public double morn { get; set; }
    }

    public class Weather
    {
        public int id { get; set; }
        public string main { get; set; }
        public string description { get; set; }
        public string icon { get; set; }
    }

    public class List
    {
        public int dt { get; set; }
        public Temp temp { get; set; }
        public double pressure { get; set; }
        public int humidity { get; set; }
        public IList<Weather> weather { get; set; }
        public double speed { get; set; }
        public int deg { get; set; }
        public int clouds { get; set; }
        public double? rain { get; set; }
    }

    public class WeatherInfo
    {
        public City city { get; set; }
        public string cod { get; set; }
        public double message { get; set; }
        public int cnt { get; set; }
        public IList<List> list { get; set; }
    }

}

So i am converting the UNIX date into a normal date. However only the UNIX date is still being displayed. Any help would be appreciated, thanks

edit - extra c# code:

public partial class _Default : System.Web.UI.Page
{

    //string city_name;
    string temp_min;
    string temp_max;
    string humidity;
    DateTime weatherdate;


    protected void Page_Load(object sender, EventArgs e)
    {

    }




    protected void reptrData_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        //Label lblCity = e.Item.FindControl("lblCity_Country") as Label;
        //city_name = lblCity.Text;

        Label lblHumidity = e.Item.FindControl("Label_humidity") as Label;
        humidity = lblHumidity.Text;

        Label LblMin = e.Item.FindControl("Label_min") as Label;
        humidity = lblHumidity.Text;

        Label LblMax = e.Item.FindControl("Label_max") as Label;
        temp_max = lblHumidity.Text;

        Label LblDate = e.Item.FindControl("DateWeather") as Label;
        weatherdate = Convert.ToDateTime(LblDate.Text);


    }

1 个答案:

答案 0 :(得分:1)

在更新之前,您是绑定数据源。请参阅下面指定的区域

#region dataBind
 Repeater_weatherReports.DataSource = weatherinfo.list;
            Repeater_weatherReports.DataBind();
#endregion
#region dataChange
            foreach (List list in weatherinfo.list)
------
#endregion

操纵后绑定数据