ConvertTo-Json:Converts an object to a JSON-formatted string.
ConvertFrom-Json:Converts a JSON-formatted string to an custom object.
请看下面的例子。
(Get-UICulture).Calendar |ConvertTo-Json
$t=Invoke-WebRequest -uri http://wthrcdn.etouch.cn/weather_mini?city=北京 |ConvertTo-Json
JSON.parse:Converts a JSON-formatted string to a JavaScript object.
JSON.stringify:Converts JavaScript object to a JSON-formatted string.
var strJson = '{"data":{"yesterday":{"date":"31日星期二","high":"高温 16℃","fx":"南风","low":"低温 4℃","fl":"","type":"多云"},"city":"北京","aqi":"70","forecast":[{"date":"1日星期三","high":"高温 17℃","fengli":"","low":"低温 6℃","fengxiang":"南风","type":"晴"},{"date":"2日星期四","high":"高温 17℃","fengli":"","low":"低温 3℃","fengxiang":"东北风","type":"阴"},{"date":"3日星期五","high":"高温 14℃","fengli":"","low":"低温 2℃","fengxiang":"北风","type":"晴"},{"date":"4日星期六","high":"高温 15℃","fengli":"","low":"低温 3℃","fengxiang":"西南风","type":"晴"},{"date":"5日星期天","high":"高温 16℃","fengli":"","low":"低温 5℃","fengxiang":"南风","type":"多云"}],"ganmao":"将有一次强降温过程,极易发生感冒,请特别注意增加衣服保暖防寒。","wendu":"19"},"status":1000,"desc":"OK"}';
var obj = JSON.parse(strJson); //将字符串生成JavaScript对象
console.log('北京 ' + obj.data.forecast[0].date + '的温度是 ' + obj.data.forecast[0].high + '.')
Json 模块的 loads 方法可以将 json 转换成 Python 字典数据。
#JSON,将json 转换成 Python 字典
import json
import requests
url = 'http://wthrcdn.etouch.cn/weather_mini?city=%E5%8C%97%E4%BA%AC'
resp = requests.get(url)
weather = json.loads(resp.text)
wh = weather['data']['forecast'][0]
print("%s%s的最高气温是 %s,最低气温是%s." % (weather['data']['city'], wh['date'][0:2],wh['high'].split(" ")[1], wh['low'].split(" ")[1]))
PowerShell 由JSON成的.net对象,访问其中的数据只支持点号访问, $v = obj.property。
JavaScript 由JSON成的JavaScript对象,访问其中的数据既支持点号又支持中括号,var value = obj.property; var v = obj['property']
Python 由JSON成的dict数据类型的对象,访问其中的数据只支持中括号访问, v = obj['property'],因为返回的是dict数据类型。