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/sh

if [ -n "$1" ]; then                                                                                                                     
  file_name=$1
else
  file_name="/data/device.json"
fi

if [ -n "$2" ]; then                                                                                                                     
  file_path=$2
else
  file_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_name

total=`free|grep "Mem" |awk '{print $2}'`
free=`free|grep "Mem" |awk '{print $4}'`
echo "  { \"total\": \"$total\", \"free\": \"$free\"}" >> $file_name

echo "]," >> $file_name
#----------------------------------------------------------------------------#
i=0

file="/proc/stat"
echo "\"cpu\":" >> $file_name
echo "[" >> $file_name

if [ ! -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 ]];do
    echo $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_name

send_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 ]
then
   echo "  { \"id\": \"$eth_name\", \"ip\": \"$ip\", \"band\": \"$band\", \"traffic\": \"$total M\"}," >> $file_name
else
   echo "  { \"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 ]];do
        k=`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 ]
        then
                echo "  { \"id\":\"$i\", \"freq\":\"$freq\", \"qam\":\"$qam\", \"stat\":\"$stat\", \"ser\":\"$ser\", \"snr\":\"$snr\", \"sigint\":\"$sigint\"}," >> $file_name
        else
                echo "  { \"id\":\"$i\", \"freq\":\"$freq\", \"qam\":\"$qam\", \"stat\":\"$stat\", \"ser\":\"$ser\", \"snr\":\"$snr\", \"sigint\":\"$sigint\"}" >> $file_name
        fi
        let "i++"
done

echo "]," >> $file_name
fi

#----------------------------------------------------------------------------#

file="/data/userinfo"

if [ ! -d "$file" ]; then

i=`cat $file | grep ":" | wc -l`
j=1
echo "\"terminal\":" >> $file_name
echo "[" >> $file_name
while [[ $j -le $i ]];do
    echo $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" ]; then
	upt=`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 ]
	then
	upt=`echo "$upt" | cut -d "K" -f 1`
	upt=`expr $upt \* 1024`
	fi
	echo "$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 ]
	then
	dnt=`echo "$dnt" | cut -d "K" -f 1`
	dnt=`expr $dnt \* 1024`
	fi
	echo "$dnt" |grep -q "$ch1"                                  
	if [ $? -eq 0 ]                    
	then                               
	dnt=`echo "$dnt" | cut -d "M" -f 1`                                           
	dnt=`expr $dnt \* 1024 \* 1024`            
	fi  
else
	upt="0.3M"
	dnt="1.2M"
fi

echo "  {" >> $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_name

if [ `expr $i - $j` != 0 ]
then
   echo "  }," >> $file_name
else
   echo "  }" >> $file_name
fi
let "j++"
done  
echo "]" >> $file_name
fi
#----------------------------------------------------------------------------#

echo "}" >> $file_name

猜你喜欢

转载自blog.csdn.net/u010872301/article/details/80525766