怎么在linux里安装json

怎么在linux里安装json,第1张

1, 下载php-json-ext-1.2.1.tar.bz2, 这个不好下载,按网上的地址很多不能下载,后来终于找到一个

2,解压缩 #tar xvjf php-json-ext-1.2.1.tar.bz2

3, 进源码目录#cd php-json-ext-1.2.1

4、准备编译环境。这一步可以先不做直接进行第5步 *** 作,如果遇到问题后再进行本步检查也无所谓。不过为了避免不必要的麻烦,我们还是检查下编译环境是否准备好了吧!

#yum install php-devel automake autoconf libtool gcc

5、准备PHP模块的编译环境:

#phpize

6、开始编译json模块:

#./configure

#make

#make install

7、配置php.ini。首先打开php.ini文件

首先安装libjson的c库

#apt-get install libjson0-dev libjson0

安装好后查看/usr/include/json下是否有头文件,有就对了!

gcc -o json-demo -g json-demo.c -std=c99 -I/usr/include/json -L/usr/lib/i386-linux-gnu/ -ljson

注意:

1、json-demo和json-demo.c参数需要在前面,不能在后面,见错误1

2、需要就爱如-std=c99,代码中需要,也可以用-std=gnu99,见错误2

3、-I是json的头文件的路径

4、-L是json库所在位置,这个路径可能根据系统不同而不同,建议用find / -name "libjson*"找到位置

5、-ljson不可省略,尽量放在最后,没有这个参数会出现错误1的现象

一、从字符串中读取JSON

#include <iostream>

#include "json/json.h"

using namespace std

int main()

{

//字符串

const char * str =

"{\"praenomen\":\"Gaius\",\"nomen\":\"Julius\",\"cognomen\":\"Caezar\","

"\"born\":-100,\"died\":-44}"

Json::Reader reader

Json::Value root

//从字符串中读取数据

if (reader.parse(str,root))

{

string praenomen = root[ "praenomen" ].asString()

string nomen = root[ "nomen" ].asString()

string cognomen = root[ "cognomen" ].asString()

int born = root[ "born" ].asInt()

int died = root[ "died" ].asInt()

cout <<praenomen + " " + nomen + " " + cognomen

<<" was born in year " <<born

<<", died in year " <<died <<endl

}

return 0

}

makefile文件

LIB=-L /usr/local/lib/libjson/ -ljson_linux- gcc -4.4.7_libmt

a: a.o

g++ -o a -std=c++0x a.o $(LIB)

a.o: a.cpp

g++ -c a.cpp

clean:

rm -rf a.o a

二、从文件中读取JSON

PersonalInfo.json(一个存储了JSON格式字符串的文件)

{

"name" : "Tsybius" ,

"age" :23,

"sex_is_male" : true ,

"partner" :

{

"partner_name" : "Galatea" ,

"partner_age" :21,

"partner_sex_is_male" : false

},

"achievement" :[ "ach1" , "ach2" , "ach3" ]

}

#include <iostream>

#include <fstream>

#include "json/json.h"

using namespace std

int main()

{

Json::Reader reader

Json::Value root

//从文件中读取

ifstream is

is.open( "PersonalInfo.json" , ios::binary)

if (reader.parse(is,root))

{

//读取根节点信息

string name = root[ "name" ].asString()

int age = root[ "age" ].asInt()

bool sex_is_male = root[ "sex_is_male" ].asBool()

cout <<"My name is " <<name <<endl

cout <<"I'm " <<age <<" years old" <<endl

cout <<"I'm a " <<(sex_is_male ? "man" : "woman" ) <<endl

//读取子节点信息

string partner_name = root[ "partner" ][ "partner_name"].asString()

int partner_age = root[ "partner" ][ "partner_age" ].asInt()

bool partner_sex_is_male = root[ "partner" ]["partner_sex_is_male" ].asBool()

cout <<"My partner's name is " <<partner_name <<endl

cout <<(partner_sex_is_male ? "he" : "she" ) <<" is "

<<partner_age <<" years old" <<endl

//读取数组信息

cout <<"Here's my achievements:" <<endl

for ( int i = 0i <root[ "achievement" ].size()i++)

{

string ach = root[ "achievement" ][i].asString()

cout <<ach <<'\t'

}

cout <<endl

cout <<"Reading Complete!" <<endl

}

is.close()

return 0

}

makefile

?

LIB=-L /usr/local/lib/libjson/ -ljson_linux- gcc -4.4.7_libmt

a: a.o

g++ -o a -std=c++0x a.o $(LIB)

a.o: a.cpp

g++ -c a.cpp

clean:

rm -rf a.o a

三、将信息保存为JSON格式

a.cpp

#include <iostream>

#include <fstream>

#include "json/json.h"

using namespace std

int main()

{

//根节点

Json::Value root

//根节点属性

root[ "name" ] = Json::Value( "Tsybius" )

root[ "age" ] = Json::Value(23)

root[ "sex_is_male" ] = Json::Value( true )

//子节点

Json::Value partner

//子节点属性

partner[ "partner_name" ] = Json::Value( "Galatea" )

partner[ "partner_age" ] = Json::Value(21)

partner[ "partner_sex_is_male" ] = Json::Value( false )

//子节点挂到根节点上

root[ "partner" ] = Json::Value(partner)

//数组形式

root[ "achievement" ].append( "ach1" )

root[ "achievement" ].append( "ach2" )

root[ "achievement" ].append( "ach3" )

//直接输出

cout <<"FastWriter:" <<endl

Json::FastWriter fw

cout <<fw.write(root) <<endl <<endl

//缩进输出

cout <<"StyledWriter:" <<endl

Json::StyledWriter sw

cout <<sw.write(root) <<endl <<endl

//输出到文件

ofstream os

os.open( "PersonalInfo" )

os <<sw.write(root)

os.close()

return 0

}

makefile

LIB=-L /usr/local/lib/libjson/ -ljson_linux- gcc -4.4.7_libmt

a: a.o

g++ -o a -std=c++0x a.o $(LIB)

a.o: a.cpp

g++ -c a.cpp

clean:

rm -rf a.o a

{

"achievement" : [ "ach1" , "ach2" , "ach3" ],

"age" : 23,

"name" : "Tsybius" ,

"partner" : {

"partner_age" : 21,

"partner_name" : "Galatea" ,

"partner_sex_is_male" : false

},

"sex_is_male" : true

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/7085274.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-01
下一篇2023-04-01

发表评论

登录后才能评论

评论列表(0条)

    保存