JOSN文件解析

2024-05-24 05:08
文章标签 解析 josn

本文主要是介绍JOSN文件解析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式。一般通过网页将数据下发,通过C语言或shell脚本,来实现数据的解析交换等功能!,可以通过JSON在线视图查看器来查看JOSN数据格式,如下是一段JOSN数据:

192.168.0.21 - - [24/Nov/2016:00:28:01 +0800] "GET /auth/ping/index.html?gw_id=default&sys_uptime=17886&sys_memfree=257860&sys_load=0.34&wifidog_uptime=557952 HTTP/1.0" 200 4 
192.168.0.21 - - [24/Nov/2016:00:28:10 +0800] "GET /auth/auth/index.html?stage=counters&ip=192.168.0.186&mac=44:6d:57:b3:15:72&token=1100169&incoming=39684267&outgoing=7140946&gw_id=default HTTP/1.0" 200 7
192.168.0.21 - - [24/Nov/2016:00:28:10 +0800] "GET /auth/auth/index.html?stage=counters&ip=192.168.0.240&mac=d0:7e:35:d3:32:cc&token=6662206&incoming=168168594&outgoing=3575532&gw_id=default HTTP/1.0" 200 7
通过C语言进行解析:
#include <stdio.h>
#include <stdlib.h>FILE *in, *out;void read_file()
{char ch;char buf[1024];	int i = 0, size = 0;char msg0[] = " {\"deviceGUID\" : \"C48RNGBQ1GKB4A41\",\"timestamp\" : \"2016-12-30 02:00:00\",\"appData\":[{ \"id\": 0 ";fwrite(msg0,strlen(msg0),1,out);//把字符串内容写入到文件while ((ch = fgetc(in)) != EOF){char msg[] = " \",\"type\": \"AD\",\"appKey\": \"portal\",\"data:\"{ ";fwrite(msg,strlen(msg),1,out);//把字符串内容写入到文件fseek(out,0,SEEK_END);//定位文件指针到文件开始位置while ((ch = fgetc(in)) != '\n'){		buf[i++]=ch;  fwrite(&ch, 1, 1, out);  size++;}char msg1[] = " }\",";fwrite(msg1,strlen(msg1),1,out);//把字符串内容写入到文件fseek(out,0,SEEK_END);//定位文件指针到文件开始位置char msg2[] = " }\]}";fwrite(msg2,strlen(msg2),1,out);//把字符串内容写入到文件}
}int main()
{	if ((in = fopen("access_log","r")) == NULL) //in.txt 和out.txt 都在当前工作目录下存放{printf("canot find the access_log file!\n");exit(0);}if ((out = fopen("out_txt","w+"))==NULL) // 写入数据的文件{printf("canot find the out_txt file!\n");exit(0);}read_file();fclose(in); // 关闭文件fclose(out);puts("");return 0;
}

通过Shell脚本实现:

#!/bin/shif [ -n "$1" ]; then                                                                                                                     file_name=$1
elsefile_name="/data/device.json"
fiif [ -n "$2" ]; then                                                                                                                     file_path=$2
elsefile_path="/data/type.txt"
fi#echo "" > $file_name
echo "" > $file_name
echo "{" >> $file_name#----------------------------------------------------------------------------#
echo "\"deviceGUID\":\"C48RNGBQ1GKB4A41\"," >> $file_name#----------------------------------------------------------------------------#
timestamp=`date "+%G-%m-%d %H:%M:%S"`
echo "\"timestamp\":\"$timestamp\"," >> $file_name#----------------------------------------------------------------------------#
sysload=`uptime |awk '{print $10}'`
sysload=`echo ${sysload%%,*}`
echo "\"sysload\":\"$sysload\"," >> $file_name#----------------------------------------------------------------------------#
echo "\"mem\":" >> $file_name
echo "[" >> $file_nametotal=`free|grep "Mem" |awk '{print $2}'`
free=`free|grep "Mem" |awk '{print $4}'`
echo "  { \"total\": \"$total\", \"free\": \"$free\"}" >> $file_nameecho "]," >> $file_name
#----------------------------------------------------------------------------#
i=0file="/proc/stat"
echo "\"cpu\":" >> $file_name
echo "[" >> $file_nameif [ ! -d "$file" ]; then
user1=`cat $file|sed -n "1p"|awk '{print $2}'`
nice1=`cat $file|sed -n "1p"|awk '{print $3}'`
system1=`cat $file|sed -n "1p"|awk '{print $4}'`
idle1=`cat $file|sed -n "1p"|awk '{print $5}'`
iowait1=`cat $file|sed -n "1p"|awk '{print $6}'`
irq1=`cat $file|sed -n "1p"|awk '{print $7}'`
softirq1=`cat $file|sed -n "1p"|awk '{print $8}'`
stealstolen1=`cat $file|sed -n "1p"|awk '{print $9}'`
guest1=`cat $file|sed -n "1p"|awk '{print $10}'`total1=`expr $user1 + $nice1 + $system1 + $idle1 + $iowait1 + $irq1 + $softirq1 + $stealstolen1 + $guest1`user2=`cat $file|sed -n "1p"|awk '{print $2}'`
nice2=`cat $file|sed -n "1p"|awk '{print $3}'`
system2=`cat $file|sed -n "1p"|awk '{print $4}'`
idle2=`cat $file|sed -n "1p"|awk '{print $5}'`
iowait2=`cat $file|sed -n "1p"|awk '{print $6}'`
irq2=`cat $file|sed -n "1p"|awk '{print $7}'`
softirq2=`cat $file|sed -n "1p"|awk '{print $8}'`
stealstolen2=`cat $file|sed -n "1p"|awk '{print $9}'`
guest2=`cat $file|sed -n "1p"|awk '{print $10}'`total2=`expr $user2 + $nice2 + $system2 + $idle2 + $iowait2 + $irq2 + $softirq2 + $stealstolen2 + $guest2`total=`expr $total2 - $total1`
idle=`expr $idle2 - $idle1`use=`expr $total - $idle`
total=`expr $use \* 100`load=$(echo $use $total | awk '{ printf "%0.1f\n" ,$1/$2}')#load=`top -n 1 |grep Cpu | cut -d "," -f 1 | cut -d ":" -f 2|awk '{print $2}'`
#load=`top -n 1 |grep Cpu | cut -d "," -f 1 | cut -d ":" -f 2 | cut -d "," -f 1| cut -d " " -f 3`
#load=`top -n 1 |grep CPU |awk '{print $2}'|sed -n "1p"`
#load=`top -n 1 | awk '/CPU/{print $2}'|sed -n "1p"`
#load=`echo ${load%\,*}`echo "  { \"id\": \"$i\", \"load\":\"$load%\"}" >> $file_name
fi
echo "]," >> $file_name
#----------------------------------------------------------------------------#
file="/data/type.txt"if [ ! -d "$file" ]; then
type=`cat /data/type.txt|sed -n "1p"`total_M=`df -m| grep mntWRT|awk '{print $1}'`
used_M=`df -m| grep mntWRT|awk '{print $2}'`
free_M=`df -m| grep mntWRT|awk '{print $3}'`a=1024
total=$(echo $total_M $a | awk '{ printf "%0.1f\n" ,$1/$2}') 
used=$(echo $used_M $a | awk '{ printf "%0.1f\n" ,$1/$2}')
free=$(echo $free_M $a | awk '{ printf "%0.1f\n" ,$1/$2}')echo "\"storage\":" >> $file_name
echo "[" >> $file_name
echo "  { \"id\": \"$type\", \"total\":\"$total G\", \"used\":\"$used G\", \"free\":\"$free G\"}" >> $file_name
echo "]," >> $file_name
fi
echo "" >> $file_name
#----------------------------------------------------------------------------#
i=`cat /proc/net/dev | grep ":" | awk '{print $1}'|wc -l`
j=2
echo "\"nic\":" >> $file_name
echo "[" >> $file_name
#while [[ $j -ge $i ]];do
#for((j=1;j<=$i;j++));do
while [[ $j -le $i ]];doecho $j;
eth_name=`cat /proc/net/dev | grep ":" | awk '{print $1}'|cut -d ":" -f 1|sed -n ""$j"p"`if [ $eth_name != "lo" ];then
ip=`ifconfig $eth_name| grep 'inet addr:'|sed 's/.*inet addr://g'| cut -d " " -f 1`
#echo "ip:$ip" >> $file_name
#echo "ip:$ip"band=`ethtool $eth_name| grep Speed | awk '{print $2}'|cut -d "b" -f 1`
#echo "band: $band" >> $file_namesend_o=`ifconfig $eth_name | grep bytes | awk '{print $6}' | awk -F : '{print $2}'`
recv_o=`ifconfig $eth_name | grep bytes | awk '{print $2}' | awk -F : '{print $2}'`
sleep 1
send_n=`ifconfig $eth_name | grep bytes | awk '{print $6}' | awk -F : '{print $2}'`
recv_n=`ifconfig $eth_name | grep bytes | awk '{print $2}' | awk -F : '{print $2}'`send_r=`expr $send_n - $send_o`
recv_r=`expr $recv_n - $recv_o`
total_r=`expr $send_r + $recv_r`echo  "Send rate: $send_o Bytes/sec  Recv rate: $recv_o Bytes/sec Total rate: $total_r Bytes/sec"a=1024#total=`echo "sclae=2;$total_r/1024" | bc`
total=$(echo $total_r $a | awk '{ printf "%0.1f\n" ,$1/$2}')if [ `expr $i - $j` != 0 ]
thenecho "  { \"id\": \"$eth_name\", \"ip\": \"$ip\", \"band\": \"$band\", \"traffic\": \"$total M\"}," >> $file_name
elseecho "  { \"id\": \"$eth_name\", \"ip\": \"$ip\", \"band\": \"$band\", \"traffic\": \"$total M\"}" >> $file_name
fi
#j=$(($j+1))
fi
let "j++"
last_eth_name=$eth_name
done  
echo "]," >> $file_name
echo "" >> $file_name#----------------------------------------------------------------------------#
#----------------------------------------------------------------------------#
file="/services/data/tunerinfo.txt"if [ ! -d "$file" ]; then
line=`cat $file|grep ","|wc -l`i=0
j=`expr $line - 1` echo "\"tuner\":" >> $file_name
echo "[" >> $file_name
while [[ $i -le $j ]];dok=`expr $i + 1`freq=`cat $file|cut -d "," -f 1|sed -n ""$k"p"`qam=`cat $file|cut -d "," -f 2|sed -n ""$k"p"`stat=`cat $file|cut -d "," -f 3|sed -n ""$k"p"`ser=`cat $file|cut -d "," -f 4|sed -n ""$k"p"`snr=`cat $file|cut -d "," -f 5|sed -n ""$k"p"`sigint=`cat $file|cut -d "," -f 6|sed -n ""$k"p"`if [ `expr $i - $j` != 0 ]thenecho "  { \"id\":\"$i\", \"freq\":\"$freq\", \"qam\":\"$qam\", \"stat\":\"$stat\", \"ser\":\"$ser\", \"snr\":\"$snr\", \"sigint\":\"$sigint\"}," >> $file_nameelseecho "  { \"id\":\"$i\", \"freq\":\"$freq\", \"qam\":\"$qam\", \"stat\":\"$stat\", \"ser\":\"$ser\", \"snr\":\"$snr\", \"sigint\":\"$sigint\"}" >> $file_namefilet "i++"
doneecho "]," >> $file_name
fi#----------------------------------------------------------------------------#file="/data/userinfo"if [ ! -d "$file" ]; theni=`cat $file | grep ":" | wc -l`
j=1
echo "\"terminal\":" >> $file_name
echo "[" >> $file_name
while [[ $j -le $i ]];doecho $j;id=`expr $j - 1`
macid=`cat $file | grep ":" | cut -d "," -f 1|sed -n ""$j"p"`ip=`cat $file | grep ":" |cut -d "," -f 2|sed -n ""$j"p"`ctime=`cat $file | grep ":" | cut -d "," -f 5|sed -n ""$j"p"`cpnum=19132312345
platform=`cat $file | grep ":" | cut -d "," -f 3|sed -n ""$j"p"`
model=`cat $file | grep ":" | cut -d "," -f 3|sed -n ""$j"p"`
ch="K"
ch1="M"
if [ -d "/data/traffic.txt" ]; thenupt=`cat /data/traffic.txt|grep $ip|cut -d "," -f 2`dnt=`cat /data/traffic.txt|grep $ip|cut -d "," -f 3`echo "$upt" |grep -q "$ch"if [ $? -eq 0 ]thenupt=`echo "$upt" | cut -d "K" -f 1`upt=`expr $upt \* 1024`fiecho "$upt" |grep -q "$ch1"if [ $? -eq 0 ]        then            upt=`echo "$upt" | cut -d "M" -f 1`                                           upt=`expr $upt \* 1024 \* 1024`            fi echo "$dnt" |grep -q "$ch"if [ $? -eq 0 ]thendnt=`echo "$dnt" | cut -d "K" -f 1`dnt=`expr $dnt \* 1024`fiecho "$dnt" |grep -q "$ch1"                                  if [ $? -eq 0 ]                    then                               dnt=`echo "$dnt" | cut -d "M" -f 1`                                           dnt=`expr $dnt \* 1024 \* 1024`            fi  
elseupt="0.3M"dnt="1.2M"
fiecho "  {" >> $file_name
echo "    \"id\": \"$id\"," >> $file_name
echo "    \"ctime\": \"$ctime\"," >> $file_name
echo "    \"macid\": \"$macid\"," >> $file_name
echo "    \"ip\": \"$ip\"," >> $file_name
echo "    \"cpnum\": \"$cpnum\"," >> $file_name
echo "    \"platform\": \"$platform\"," >> $file_name
echo "    \"model\": \"$model\"," >> $file_name
echo "    \"upt\": \"$upt\"," >> $file_name
echo "    \"dnt\": \"$dnt\"" >> $file_nameif [ `expr $i - $j` != 0 ]
thenecho "  }," >> $file_name
elseecho "  }" >> $file_name
fi
let "j++"
done  
echo "]" >> $file_name
fi
#----------------------------------------------------------------------------#echo "}" >> $file_name

这篇关于JOSN文件解析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/997460

相关文章

nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析(结合应用场景)

《nginx-t、nginx-sstop和nginx-sreload命令的详细解析(结合应用场景)》本文解析Nginx的-t、-sstop、-sreload命令,分别用于配置语法检... 以下是关于 nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析,结合实际应

MyBatis中$与#的区别解析

《MyBatis中$与#的区别解析》文章浏览阅读314次,点赞4次,收藏6次。MyBatis使用#{}作为参数占位符时,会创建预处理语句(PreparedStatement),并将参数值作为预处理语句... 目录一、介绍二、sql注入风险实例一、介绍#(井号):MyBATis使用#{}作为参数占位符时,会

PostgreSQL的扩展dict_int应用案例解析

《PostgreSQL的扩展dict_int应用案例解析》dict_int扩展为PostgreSQL提供了专业的整数文本处理能力,特别适合需要精确处理数字内容的搜索场景,本文给大家介绍PostgreS... 目录PostgreSQL的扩展dict_int一、扩展概述二、核心功能三、安装与启用四、字典配置方法

深度解析Java DTO(最新推荐)

《深度解析JavaDTO(最新推荐)》DTO(DataTransferObject)是一种用于在不同层(如Controller层、Service层)之间传输数据的对象设计模式,其核心目的是封装数据,... 目录一、什么是DTO?DTO的核心特点:二、为什么需要DTO?(对比Entity)三、实际应用场景解析

深度解析Java项目中包和包之间的联系

《深度解析Java项目中包和包之间的联系》文章浏览阅读850次,点赞13次,收藏8次。本文详细介绍了Java分层架构中的几个关键包:DTO、Controller、Service和Mapper。_jav... 目录前言一、各大包1.DTO1.1、DTO的核心用途1.2. DTO与实体类(Entity)的区别1

Java中的雪花算法Snowflake解析与实践技巧

《Java中的雪花算法Snowflake解析与实践技巧》本文解析了雪花算法的原理、Java实现及生产实践,涵盖ID结构、位运算技巧、时钟回拨处理、WorkerId分配等关键点,并探讨了百度UidGen... 目录一、雪花算法核心原理1.1 算法起源1.2 ID结构详解1.3 核心特性二、Java实现解析2.

使用Python绘制3D堆叠条形图全解析

《使用Python绘制3D堆叠条形图全解析》在数据可视化的工具箱里,3D图表总能带来眼前一亮的效果,本文就来和大家聊聊如何使用Python实现绘制3D堆叠条形图,感兴趣的小伙伴可以了解下... 目录为什么选择 3D 堆叠条形图代码实现:从数据到 3D 世界的搭建核心代码逐行解析细节优化应用场景:3D 堆叠图

深度解析Python装饰器常见用法与进阶技巧

《深度解析Python装饰器常见用法与进阶技巧》Python装饰器(Decorator)是提升代码可读性与复用性的强大工具,本文将深入解析Python装饰器的原理,常见用法,进阶技巧与最佳实践,希望可... 目录装饰器的基本原理函数装饰器的常见用法带参数的装饰器类装饰器与方法装饰器装饰器的嵌套与组合进阶技巧

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

全面解析MySQL索引长度限制问题与解决方案

《全面解析MySQL索引长度限制问题与解决方案》MySQL对索引长度设限是为了保持高效的数据检索性能,这个限制不是MySQL的缺陷,而是数据库设计中的权衡结果,下面我们就来看看如何解决这一问题吧... 目录引言:为什么会有索引键长度问题?一、问题根源深度解析mysql索引长度限制原理实际场景示例二、五大解决