-2

I have this JSON data i'm receiving from my webservice, the problem now is that i'm currently trying to retrieve the data outside square bracket, because my current codes is only able to retrieve the weather data.

And whenever i try to get the data from coord it keeps returning me the value 0.

 {  
       "coord":{  
          "lon":145.77,
          "lat":-16.92
       },
       "weather":[  
          {  
             "id":802,
             "main":"Clouds",
             "description":"scattered clouds",
             "icon":"03n"
          }
       ],
       "base":"stations",
       "main":{  
          "temp":300.15,
          "pressure":1007,
          "humidity":74,
          "temp_min":300.15,
          "temp_max":300.15
       },
       "visibility":10000,
       "wind":{  
          "speed":3.6,
          "deg":160
       },
       "clouds":{  
          "all":40
       },
       "dt":1485790200,
       "sys":{  
          "type":1,
          "id":8166,
          "message":0.2064,
          "country":"AU",
          "sunrise":1485720272,
          "sunset":1485766550
       },
       "id":2172797,
       "name":"Cairns",
       "cod":200
    }

This is my current code i am trying to get the data from the curly bracket coord but it keeps giving 0. Any help would be appreciated thanks!

public partial class Book : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public class Result
    {
        public string id{ get; set; }
        public string main{ get; set; }
        public string description{ get; set; }
        public string icon{ get; set; }
        public decimal lon{ get; set; }

    }

    public class SearchList
    {
        public int resultCount;
        public Result[] weather;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string searchTerm = TextBox1.Text;
        var webRequest = (HttpWebRequest)WebRequest.Create
        ("http://samples.openweathermap.org/data/2.5/weather?id=2172797&appid=b6907d289e10d714a6e88b30761fae22");
        var webResponse = (HttpWebResponse)webRequest.GetResponse();
        if (webResponse.StatusCode == HttpStatusCode.OK)
        {
            JavaScriptSerializer json = new JavaScriptSerializer();
            StreamReader sr = new StreamReader(webResponse.GetResponseStream());
            string resString = sr.ReadToEnd();
            SearchList list = json.Deserialize<SearchList>(resString);
            GridView1.DataSource = list.weather;
            GridView1.DataBind();
        }
        else
            Label1.Text = "Invalid Response";
    }
}
Best Jeanist
  • 1,109
  • 4
  • 14
  • 34

4 Answers4

1

Your models are incomplete. You can use JsonCsharp to generate classes.

public class Coord
{
    public double lon { get; set; }
    public double lat { 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 SearchList
{
    public Coord coord { get; set; }
    public List<Weather> weather { get; set; }
    public int resultCount;
}
Bruno Caceiro
  • 7,035
  • 1
  • 26
  • 45
  • when i add your code it still only shows me the `weather` data. Am i still suppose to just do it like this? `SearchList list = json.Deserialize(resString); GridView1.DataSource = list.weather;` – Best Jeanist Jan 24 '19 at 16:43
  • You are setting the dataSource to list.weather. You need to set DataSource= list , to have both weather and coord – Bruno Caceiro Jan 24 '19 at 16:45
  • You mean like this `GridView1.DataSource = list;` it still give me an error `data source is an invalid type` – Best Jeanist Jan 24 '19 at 16:49
  • @BestJeanist based on your responses this looks like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). You need to clarify in the original question what is the actual problem – Nkosi Jan 24 '19 at 16:57
  • @BestJeanist As stated by Nkosi, you have a different problem now. In object list you have your Coord object and your list of weather. This was yoour problem. now you need to adapt the binding to display a list of weather and an object of type coord. – Bruno Caceiro Jan 24 '19 at 17:01
  • @BrunoCaceiro How do i do that? – Best Jeanist Jan 24 '19 at 17:06
  • Just ask a different question. And provide the code for the GridView1 – Bruno Caceiro Jan 24 '19 at 17:07
0

You were only able to retrieve the weather data because it was defined in the object model.

Update SearchList class definition to include the other desired data

public class SearchList {
    public Position coord { get; set; }
    public int resultCount { get; set; }
    public Result[] weather { get; set; }
}

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

That way the JavaScriptSerializer will know to include it when deserializing the JSON.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
0

You need to add the model for coord

public class SearchList
{
    public Coord coord;
    public int resultCount;
    public Result[] weather;
}

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

You will need to add more models for each type of data. You cannot simply access list.coord.lon from list.weather.lon

0

use JObject then you can extract each Token using

`Jobject.SelectToken("datayouwant")`.
Emotional_Goose
  • 167
  • 1
  • 13