php - 如何将特定的 json 数据加入到一个 txt 文件中
问题描述
<?php
$weatherdata = file_get_contents('http://api.openweathermap.org/data/2.5/weather?id=appkey');
file_put_contents('weatherdata.json', $weatherdata);
$decoded = json_decode($weatherdata);
file_put_contents('weatherdata_alert.txt', $decoded->weather[0]->description);
?>
我可以将 json 数据中的描述转换为标题为“weatherdata_alert.txt”的文本,但似乎无法包含字符串中的其他数据。这是期望的结果“晴朗的天空 2° Millbrook”描述为“晴朗的天空”
这是json数据
{
"coord": {
"lon": -78.45,
"lat": 44.15
},
"weather": [{
"id": 804,
"main": "Clouds",
"description": "overcast clouds",
"icon": "04n"
}],
"base": "stations",
"main": {
"temp": 272.97,
"feels_like": 269.29,
"temp_min": 272.04,
"temp_max": 274.15,
"pressure": 1021,
"humidity": 69
},
"visibility": 10000,
"wind": {
"speed": 1.5,
"deg": 130
},
"clouds": {
"all": 90
},
"dt": 1604459237,
"sys": {
"type": 1,
"id": 882,
"country": "CA",
"sunrise": 1604404430,
"sunset": 1604440849
},
"timezone": -18000,
"id": 7804010,
"name": "Millbrook",
"cod": 200
}
解决方案
推荐阅读
- google-api - 执行 Batch API 以将多个用户添加到多个组
- sql - 如何在此查询的这一部分中使用标签?
- vb.net - Lotusscript 将 base64 编码的字符串保存到文件 (DLL)
- mysql - 在python中插入多个数据框时没有从mysql获取值
- javascript - 如何修复此代码中的“前缀未定义”
- c# - 如何使用 ASP.NET 技术在 Facebook 墙上分享 .mp4 视频?
- r - `rhandsontable` 包中的 `hot_to_r()` 函数在“正常”.R 脚本中不起作用 - 如何检查闪亮数据的方面?
- amazon-web-services - AWS CodeBuild 事件不会触发 lambda
- java - 即使使用 sharedPreferences,AlertDialog 也永远不会显示
- node.js - ESLint 同时输出到文件和控制台