이 JSON 문자열을 "WeatherJson"유형의 개체로 구문 분석해야합니다. 그러나 ' "weather":와 같은 문자열 내부의 배열을 구문 분석하는 방법을 모르겠습니다 [{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}]
. 엔티티 클래스는 어떻게 생겼습니까?
JSON 문자열 :
{
"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
}
편집하다
코드에서 다음 값을 검색해야합니다.
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
JSON의 배열을 POCO의 목록 또는 배열 유형과 일치시켜야합니다. 다음은 제공 한 JSON을 사용하는 짧지 만 완전한 예입니다.
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; }
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다