jshn-如何解析json包 [英] jshn - how to parse json package

查看:713
本文介绍了jshn-如何解析json包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在openwrt上轻松解析json?

I was wondering how to easily parse json on openwrt?

我有jhsn来解析json,这是我的过程(.sh脚本):

I've jhsn to parse json and this is my procedure (.sh script):

#download weather
wget "api.openweathermap.org/data/2.5/weather?id=2172797&appid=44db6a862fba0b067b1930da0d769e98"
#set variable wit json data
$weather=`cat weather?`
jshn -r "$weather"

最后显示:

json_init;
json_add_object 'coord';
json_add_double 'lon' 18.650000;
json_add_double 'lat' 50.300000;
json_close_object;
json_add_array 'weather';
json_add_object '0';
json_add_int 'id' 701;
json_add_string 'main' 'Mist';
json_add_string 'description' 'mist';
json_add_string 'icon' '50n';
json_close_object;
json_close_array;
json_add_string 'base' 'cmc stations';
json_add_object 'main';
json_add_double 'temp' 262.900000;
json_add_int 'pressure' 1033;
json_add_int 'humidity' 92;
json_add_double 'temp_min' 261.150000;
json_add_double 'temp_max' 263.750000;
json_close_object;
json_add_object 'wind';
json_add_double 'speed' 3.100000;
json_add_int 'deg' 140;
json_close_object;
json_add_object 'snow';
json_close_object;
json_add_object 'clouds';
json_add_int 'all' 0;
json_close_object;
json_add_int 'dt' 1453490267;
json_add_object 'sys';
json_add_int 'type' 3;
json_add_int 'id' 5356;
json_add_double 'message' 0.010000;
json_add_string 'country' 'PL';
json_add_int 'sunrise' 1453444351;
json_add_int 'sunset' 1453476124;
json_close_object;
json_add_int 'id' 7530857;
json_add_string 'name' 'Gliwice';
json_add_int 'cod' 200;

如何过滤并仅获得一个值?例如速度值:3.100000(使用jshn或其他脚本等)?

How can I filter that and get only one value? For example speed value: 3.100000 with jshn or another script etc?

推荐答案

以下脚本可以满足我的需求

following script do what I need

#!/bin/sh

. /usr/share/libubox/jshn.sh

O=$(wget "api.openweathermap.org/data/2.5/weather?id=2172797&appid=44db6a862fba0b067b1930da0d769e98" -O -)

json_load "$O"
json_select wind
json_get_var var1 speed
echo "speed: $var1"

更多信息@ http://eko.one.pl/forum /viewtopic.php?pid=157702#p157702

这篇关于jshn-如何解析json包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆