-1

I need to parse this JSON String to my object of type "WeatherJson". However I do not know how to parse the arrays inside the string such as '"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}]. What would the entity class look like?

The JSON String:

{
  "coord": {"lon":79.85,"lat":6.93},
  "sys": {
    "type": 1, 
    "id": 7864, 
    "message": 0.0145,
    "country": "LK",
    "sunrise": 1435883361,
    "sunset": 1435928421
  },
  "weather": [
     {"id":802, "main":"Clouds", "description":"scattered clouds", "icon":"03d"}
  ],
  "base": "stations",
  "main": {
    "temp": 302.15,
    "pressure": 1013,
    "humidity": 79,
    "temp_min": 302.15,
    "temp_max": 302.15
  },
  "visibility":10000,
  "wind": { "speed": 4.1, "deg": 220 },
  "clouds": { "all": 40 },
  "dt": 1435893000,
  "id":1248991,
  "name":"Colombo",
  "cod":200
}

EDIT

I need to retrieve following values from the code:

WeatherJson w = new WeatherJson();
Console.WriteLine(w.weather.description);
//that above line was retrieved and stored from the JSONArray named 'weather' in the main json response
captainsac
  • 2,484
  • 3
  • 27
  • 48
Jay
  • 4,873
  • 7
  • 72
  • 137
  • Well what does your `WeatherJson` type look like? Do you have some code already to attempt the parsing? If so, what happens when you try it? – Jon Skeet Jul 03 '15 at 06:13
  • Take a look at this [How to parse json in C#][1] [1]: http://stackoverflow.com/a/6620173/1129313 – Garry Jul 03 '15 at 06:15
  • I do know how to parse the normal json data such as : message":0.0145. My problem is retrieving arrays and storing it in the object @JonSkeet – Jay Jul 03 '15 at 06:16
  • Well again, what have you tried? Just make them arrays or lists within your data type. (I've reformatted the JSON to make it rather easier to read, btw.) – Jon Skeet Jul 03 '15 at 06:18
  • Well that *still* hasn't shown the `WeatherJson` type, and as `weather` is an array, it should be an array in your type - so you'd need `w.Weather[0].Description`. – Jon Skeet Jul 03 '15 at 06:20

6 Answers6

5

You should just make the arrays in the JSON match with list or array types in your POCO. Here's a short but complete example using the JSON you've provided:

using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;

class Test
{ 
    static void Main(string[] args) 
    {
        var json = File.ReadAllText("weather.json");
        var root = JsonConvert.DeserializeObject<Root>(json);
        Console.WriteLine(root.Weather[0].Description);
    }
}

public class Root
{
    // Just a few of the properties
    public Coord Coord { get; set; }
    public List<Weather> Weather { get; set; }
    public int Visibility { get; set; }
    public string Name { get; set; }
}

public class Weather
{
    public int Id { get; set; }
    public string Description { get; set; }
}

public class Coord
{
    public double Lon { get; set; }
    public double Lat { get; set; }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

Try like this:

void Main()
{
    var json = System.IO.File.ReadAllText(@"d:\test.json");

    var objects = JArray.Parse(json); // parse as array  
    foreach(JObject root in objects)
    {
        foreach(KeyValuePair<String, JToken> app in root)
        {
            var appName = app.Key;
            var description = (String)app.Value["Description"];
            var value = (String)app.Value["Value"];

            Console.WriteLine(appName);
            Console.WriteLine(description);
            Console.WriteLine(value);
            Console.WriteLine("\n");
        }
    }
}
Garry
  • 4,493
  • 3
  • 28
  • 48
2

Try this hope it will help you....

      var dataObj=JSON.parse {"coord":{"lon":79.85,"lat":6.93},"sys":
      {"type":1,"id":7864,"message":0.0145,"country":"LK",
 "sunrise":1435883361,
"sun        set":1435928421},"weather":     [{"id":802,"main":"Clouds",
    "description":"scattered    clouds","icon":"03d"}],
  "base":"stations","main":{"temp":302.15,"pressure":1013,
"humidity":79,"temp_min":302.15,
 "temp_max":302.15},"visibility":10000,"wind":{"speed":4.1,"deg":220},
 "clouds":{"all":40},"dt":1435893000,"id":1248991,
  "name":"Colombo","cod":200}

to read this .
i m reading one single string so u can able to know.

var windData=dataObj.wind.speed;
 it will read value 4.1 from your string like this..

If u want to read array of string then..
var weatherObj=dataObj.weather[0].description;

so it will read  description value from array.like this u can read.
Maheshvirus
  • 6,749
  • 2
  • 38
  • 40
1

Try double iteration,

for (key in parent) {
     for(key1 in parent[key]){
     //
     }
} 
Surendheran
  • 187
  • 1
  • 18
1

JObject defines method Parse for this:

JObject json = JObject.Parse(JsonString);

You might want to refer to Json.NET documentation.

If you have collection of object then you can use JArray

JArray jarr = JArray.Parse(JsonString);

For documentation you can see here.

To Convert JArry to List Just put below method. It will return what you need.

Jarray.ToObject<List<SelectableEnumItem>>()
Dharmesh
  • 107
  • 1
  • 16
0

You can use JavaScriptSerializer Class to parse the JSON to Object.

Refer this StackOverflow thread

Community
  • 1
  • 1
Shankar
  • 256
  • 2
  • 12