从网站metcheck我可以获取天气预报的json文件。 http://ws1.metcheck.com/ENGINE/v9_0/json.asp?lat=52.380&lon=0.060&Fc=Av
该文件回来了,但我无法读取该文件以获取所需的信息。我想我不了解metcheckData,predictLocation和Forecast之间的关系:
{
"metcheckData":{
"forecastLocation":{
"forecast":[
{
"temperature":"18",
"dewpoint":"13",
"rain":"0.0",
"freezinglevel":"3049",
"uvIndex":"1",
"totalcloud":"65",
"lowcloud":"55",
"medcloud":"43",
"highcloud":"11",
"humidity":"79",
"windspeed":"11",
"meansealevelpressure":"1012.77",
"windgustspeed":"17",
"winddirection":"249",
"windletter":"WSW",
"icon":"PC",
"iconName":"Partly Cloudy",
"chanceofrain":"0",
"chanceofsnow":"0",
"dayOfWeek":"4",
"weekday":"Wednesday",
"sunrise":"6:02",
"sunset":"18:09",
"cumulusBaseHeight":"540",
"stratusBaseHeight":"549",
"dayOrNight":"N",
"utcTime":"2019-07-31T19:00:00.00"
},
{
"temperature":"17",
"dewpoint":"13",
"rain":"0.1",
"freezinglevel":"3192",
"uvIndex":"0",
"totalcloud":"91",
"lowcloud":"66",
"medcloud":"39",
"highcloud":"35",
"humidity":"82",
"windspeed":"11",
"meansealevelpressure":"1013.29",
"windgustspeed":"17",
"winddirection":"245",
"windletter":"WSW",
"icon":"RO",
"iconName":"Intermittent Rain",
"chanceofrain":"47",
"chanceofsnow":"0",
"dayOfWeek":"4",
"weekday":"Wednesday",
"sunrise":"6:02",
"sunset":"18:09",
"cumulusBaseHeight":"512",
"stratusBaseHeight":"520",
"dayOrNight":"N",
"utcTime":"2019-07-31T20:00:00.00"
}
],
"continent":"",
"country":"",
"location":"52.4/0.1",
"latitude":52.4,
"longitude":0.1,
"timezone":0
}
// Many other similar array entries omitted
},
"feedCreation":"2019-07-31T20:26:10.00",
"feedCreator":"Metcheck.com",
"feedModel":"GHX5",
"feedModelRun":"00Z",
"feedModelRunInitialTime":"2019-07-31T00:00:00.00",
"feedResolution":"0.01"
}
使用 使用Newtonsoft.Json; 使用Newtonsoft.Json.Linq;
我尝试了以下代码来读取诸如特定时间的温度预报之类的信息。
JObject jo = JObject.Parse(File.ReadAllText(@"C:\temp\Weather.json", Encoding.UTF8));
Dictionary<string, List<string>> values =
jo.SelectToken("forecast", true).ToObject<Dictionary<string, List<string>>>();
foreach (var kv in values)
{
rchtxtbx_output.AppendText(kv.Value[0] + "\r");
依此类推,以kv.Value [0]为温度,我将四舍五入并获取每小时的温度。不幸的是,情况并非如此,我在
时遇到错误Dictionary<string, List<string>> values =
jo.SelectToken("forecast", true).ToObject<Dictionary<string, List<string>>>();
所以一些“预测”是不正确的。我还尝试了metcheckData.forecastLocation.forecast然后预报Forecast.forecast,但是都出错了。
请问我如何从json文件中获取数据并将其写入每小时预测的丰富文本框中。
答案 0 :(得分:2)
您要通过其父标记导航到所需的标记。然后获取令牌的子级列表。
JObject jo = JObject.Parse(File.ReadAllText(@"C:\temp\Weather.json", Encoding.UTF8));
// navigate to the token via its parents
List<JToken> items = jo.SelectToken("metcheckData", true).SelectToken("forecastLocation", true).SelectToken("forecast", true).Children().ToList();
foreach (JToken token in items)
{
string temperature = token["temperature"].Value<string>();
string dewpoint = token["dewpoint"].Value<string>();
// etc...
}
答案 1 :(得分:1)
为什么不创建映射类并将JSON解析到该对象。完成之后,您可以浏览所有属性并选择所需的所有内容。
var data = JsonConvert.DeserializeObject<Rootobject>(File.ReadAllText(@"C:\temp\Weather.json", Encoding.UTF8));
public class Rootobject
{
public Metcheckdata metcheckData { get; set; }
public DateTime feedCreation { get; set; }
public string feedCreator { get; set; }
public string feedModel { get; set; }
public string feedModelRun { get; set; }
public DateTime feedModelRunInitialTime { get; set; }
public string feedResolution { get; set; }
}
public class Metcheckdata
{
public Forecastlocation forecastLocation { get; set; }
}
public class Forecastlocation
{
public Forecast[] forecast { get; set; }
public string continent { get; set; }
public string country { get; set; }
public string location { get; set; }
public float latitude { get; set; }
public float longitude { get; set; }
public int timezone { get; set; }
}
public class Forecast
{
public string temperature { get; set; }
public string dewpoint { get; set; }
public string rain { get; set; }
public string freezinglevel { get; set; }
public string uvIndex { get; set; }
public string totalcloud { get; set; }
public string lowcloud { get; set; }
public string medcloud { get; set; }
public string highcloud { get; set; }
public string humidity { get; set; }
public string windspeed { get; set; }
public string meansealevelpressure { get; set; }
public string windgustspeed { get; set; }
public string winddirection { get; set; }
public string windletter { get; set; }
public string icon { get; set; }
public string iconName { get; set; }
public string chanceofrain { get; set; }
public string chanceofsnow { get; set; }
public string dayOfWeek { get; set; }
public string weekday { get; set; }
public string sunrise { get; set; }
public string sunset { get; set; }
public string cumulusBaseHeight { get; set; }
public string stratusBaseHeight { get; set; }
public string dayOrNight { get; set; }
public DateTime utcTime { get; set; }
}
答案 2 :(得分:1)
如果您从链接中获取json并忽略def appName = "Engage";
/**
* Get the version name from command line param
*
* @return int If the param -PversionName is present then return int value or -1
*/
def getAppName = { env ->
return (env ? appName + " ("+ env + ")" : appName);
}
/**
* Get the version name from command line param
*
* @return int If the param -PversionName is present then return int value or -1
*/
def getLauncher = { env ->
return (env ? "engage-" + env + ".nfib.org" : "engage.nfib.org");
}
android {
compileSdkVersion rootProject.ext.compileSdkVersion
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
flavorDimensions "default"
defaultConfig {
applicationId "com.nfib.engage"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
}
splits {
abi {
reset()
enable enableSeparateBuildPerCPUArchitecture
universalApk false // If true, also generate a universal APK
include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
}
}
signingConfigs {
debug {
storeFile file('debug.keystore')
storePassword 'android'
keyAlias 'androiddebugkey'
keyPassword 'android'
}
}
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
// Caution! In production, you need to generate your own keystore file.
// see https://facebook.github.io/react-native/docs/signed-apk-android.
signingConfig signingConfigs.debug
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
}
}
productFlavors {
dev {
dimension 'default'
resValue "string", "app_name", getAppName("INT")
resValue "string", "link_launcher", getLauncher("dv")
applicationIdSuffix ".dv"
manifestPlaceholders = [onesignal_app_id: "b78285eb-f1ec-46f3-9ad0-c7efe691a401",
onesignal_google_project_number: "584236827312"]
}
qa {
dimension 'default'
resValue "string", "app_name", getAppName("QA")
resValue "string", "link_launcher", getLauncher("qa")
applicationIdSuffix ".qa"
manifestPlaceholders = [onesignal_app_id: "e4280f5e-62ec-41a4-bd86-f5b94e471a36",
onesignal_google_project_number: "162802054510"]
}
ua {
dimension 'default'
resValue "string", "app_name", getAppName("UA")
resValue "string", "link_launcher", getLauncher("ua")
applicationIdSuffix ".ua"
manifestPlaceholders = [onesignal_app_id: "2ffd8dc0-9c6b-4035-999d-fc694194725a",
onesignal_google_project_number: "594905904045"]
}
prod {
dimension 'default'
resValue "string", "app_name", getAppName()
resValue "string", "link_launcher", getLauncher()
manifestPlaceholders = [onesignal_app_id: "82dcb42f-1d35-4b79-bc28-2d1d02dbda36",
onesignal_google_project_number: "601125149914"]
}
}
部分,您将获得以下信息:
forecast
由此,(对我而言)变得更加明显,{
"metcheckData": {
"forecastLocation": {
"forecast": [],
"continent": "",
"country": "",
"location": "52.4/0.1",
"latitude": 52.4,
"longitude": 0.1,
"timezone": 0
}
},
"feedCreation": "2019-07-31T20:17:52.00",
"feedCreator": "Metcheck.com",
"feedModel": "GHX5",
"feedModelRun": "00Z",
"feedModelRunInitialTime": "2019-07-31T00:00:00.00",
"feedResolution": "0.01"
}
和metacheckData
都是对象,而forecastLocation
数组是forecast
的属性。 / p>
我将使用forecastLocation
。选中this和this post
所以它可能会变成这样(未经测试):
dynamics