0

I have a problem I want to get data from Json, and the data successfully gets from the json to the variable json but when I want to send the data to WeatherData it send me a zero value. I have one class that cald "WeatherData" and I want to send to data from the json (that existing a class "jsonParse") to this class.

jsonParse

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Web.Script.Serialization;
using System.IO;
namespace weather
{
 public class jsonParse : Ijson
 {
 private string urlAdd;
 public jsonParse(string _url)
 {
 if (_url == null)
 {
 //throw exption
 }
 else
 urlAdd = _url;
 }
 public WeatherData parse()
 {
 //string json = new WebClient().DownloadString(urlAdd);
 //var obj = JsonConvert.DeserializeObject<WeatherData>(json);
 //Console.WriteLine(obj.temp);
 // WeatherData m = JsonConvert.DeserializeObject<WeatherData>(json);
 WebClient n = new WebClient();
 var json = n.DownloadString(urlAdd);
 string valueOriginal = Convert.ToString(json);
 WeatherData m = JsonConvert.DeserializeObject<WeatherData>(json);
 Console.WriteLine(m);
 return m;
 }
 }
}

WeatherData

namespace weather
{
 public class WeatherData
 {
 public WeatherData(double _temp, double _minTemp, double _maxTemp )
 {
 temp = _temp;
 minTemp = _minTemp;
 maxTemp = _maxTemp;
 }
 public double temp { get; set; }
 public double minTemp { get; set; }
 public double maxTemp { get; set; }
 public override string ToString()
 {
 return "the weather:" + temp + "minTemp is:" + minTemp + "maxTemp:" + maxTemp;
 }
 }
}

json

{"coord":{"lon":139,"lat":35},
"sys":{"country":"JP","sunrise":1369769524,"sunset":1369821049},
"weather":[{"id":804,"main":"clouds","description":"overcast clouds","icon":"04n"}],
"main":{"temp":289.5,"humidity":89,"pressure":1013,"temp_min":287.04,"temp_max":292.04},
"wind":{"speed":7.31,"deg":187.002},
"rain":{"3h":0},
"clouds":{"all":92},
"dt":1369824698,
"id":1851632,
"name":"Shuzenji",
"cod":200}
asked Apr 21, 2016 at 8:32

3 Answers 3

1

If you only care about the weather part of the json, try this -

 var o = (JArray)JObject.Parse(jsonString)["weather"];
 foreach(JToken item in o)
 {
 Console.WriteLine(((JValue)item["id"]).Value);
 Console.WriteLine(((JValue)item["main"]).Value);
 Console.WriteLine(((JValue)item["description"]).Value);
 Console.WriteLine(((JValue)item["icon"]).Value);
 }
answered Apr 21, 2016 at 9:25
Sign up to request clarification or add additional context in comments.

1 Comment

And if I want main values - temp, min temp and max temp what do I do?
1

First object in json is coord, don't see that in your model. You should change your JsonModel to deserialize. From c# class generator:

public class Coord
{
 public int lon { get; set; }
 public int lat { get; set; }
}
public class Sys
{
 public string country { get; set; }
 public int sunrise { get; set; }
 public int sunset { 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 Main
{
 public double temp { get; set; }
 public int humidity { get; set; }
 public int pressure { get; set; }
 public double temp_min { get; set; }
 public double temp_max { get; set; }
}
public class Wind
{
 public double speed { get; set; }
 public double deg { get; set; }
}
public class Rain
{
 public int __invalid_name__3h { get; set; }
}
public class Clouds
{
 public int all { get; set; }
}
public class RootObject
{
 public Coord coord { get; set; }
 public Sys sys { get; set; }
 public List<Weather> weather { get; set; }
 public Main main { get; set; }
 public Wind wind { get; set; }
 public Rain rain { get; set; }
 public Clouds clouds { get; set; }
 public int dt { get; set; }
 public int id { get; set; }
 public string name { get; set; }
 public int cod { get; set; }
} 

Where RootObject is your JsonConvert.DeserializeObject(json); So you can change class names as you like.

answered Apr 21, 2016 at 8:39

6 Comments

If you want to name properties with your name (not json names) use [JsonProperty"jsonName"] string yourName;
do i need to put all of that in "WeatherData"?
Well with deserializer i think you have to use following properties to your directed one. Mean if you need "country", you have to add class with "sys". IMO.
It throws an exception.. why is that?
What kind of the exception?
|
0

Simply you cannot Deserialize a json object to a non-matching class object. So make sure you have a model object that have atleast all the properties that JSON object have.

In this case you would need following classes that are generated from your JSON object:

public class Coord
{
 public int lon { get; set; }
 public int lat { get; set; }
}
public class Sys
{
 public string country { get; set; }
 public int sunrise { get; set; }
 public int sunset { 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 Main
{
 public double temp { get; set; }
 public int humidity { get; set; }
 public int pressure { get; set; }
 public double temp_min { get; set; }
 public double temp_max { get; set; }
}
public class Wind
{
 public double speed { get; set; }
 public double deg { get; set; }
}
public class Rain
{
 public int __invalid_name__3h { get; set; }
}
public class Clouds
{
 public int all { get; set; }
}
public class ParentModel
{
 public Coord coord { get; set; }
 public Sys sys { get; set; }
 public List<Weather> weather { get; set; }
 public Main main { get; set; }
 public Wind wind { get; set; }
 public Rain rain { get; set; }
 public Clouds clouds { get; set; }
 public int dt { get; set; }
 public int id { get; set; }
 public string name { get; set; }
 public int cod { get; set; }
}
ParentModel m = JsonConvert.DeserializeObject<ParentModel>(json);

Hope this helps.

answered Apr 21, 2016 at 8:52

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.