Rumah  >  Artikel  >  什么是json数据解析

什么是json数据解析

(*-*)浩
(*-*)浩asal
2019-05-20 09:36:276742semak imbas

json数据解析的方法:首先创建JSON文件;然后在类中包含“document.h”和“cocos-ext.h”头文件;接着通过FileUtils获得JSON文件路径,并通过Document对象解析JSON数据;最后获得不同类型的数据值即可。

什么是json数据解析

在网络通信过程中需要传输数据,JOSN数据解析的过程为:首先创建JSON文件,然后在类中包含document.h和cocos-ext.h头文件,接着通过FileUtils获得JSON文件路径,并通过Document对象解析JSON数据,最终获得不同类型的数据值。

JSON(JavaScript Object Notation)是一种轻量级的数据交换格式。它使得人们可以很容易地进行阅读和编写,同时也方便了机器进行解析和生成。JSON采用完全独立于程序语言的文本格式,但是也使用了类C语言的习惯(包括C、C++、C#、Java、JavaScript、Perl和Python等),这些特性使得JSON成为理想的数据交换语言。

JSON数据解析

例如在Cocos2d.x根目录的external/json目录下面,是JSON处理的相关类,这里主要使用document.h这个头文件,该文件中的两个核心类是GenericValue和GenericDocument,GenericDocument继承GeneficValue。使用GenericDocument用来处理文档内容,如解析文档内容;而GenericValue主要处理值内容,即文档内部的key-value键值对内容,可以根据key获得value。GenericValue和GenericDocument都被重新进行了类型定义。所以可以使用类型定义后的名称。

ypedef GenericDocument>Document;

typedef GenericValue>Value;

Value重载了数组下标操作符[],所以我们可以使用该操作符,根据JSON文件中的key获得value。

const GenericValue & operator [] (const Ch* name) const{

const_cast(*this)[name];}

Value还提供了一组GetXXX方法,根据不同的数据类型,获得相应的值。

通过一个实例来演示如何解析JSON数据,操作步骤如下: 

创建JSON文件

在项目的classes文件夹下创建一个JSON文件,内容如下:

{"pets":["dog","cat"],"stuInfo":{
"stuAge":23,"stuName":"rose","birthday":"1990-01-12"},"username","tom","other":[true,30]}

在该文件中,pets是一个数组,表示宠物,有两个值dog和pet;stuInfo是一个学生信息;后面是一个usename;最后还有一个other数组。

在类中包含document.h和cocos-ext.h头文件

#include "cocos-ext.h"
#include "json/document.h"

通过FileUtils获得JSON文件路径

const char* file_path = FileUtils::getInstance()->fullPathForFilename("hello.json").c_str();
log("文件路径path=%s",file_path);

通过Document对象解析JOSN数据

//文档对象
rapidjson::Document dl;
//获得JSON字符串内容
std::string contentStr = FileUtils::getInstance()->getStringFromFile(file_path);
//解析
dl.Parse<0>(contentStr.c_str());
//输出JSON文件的内容
printf("%s\n",contentStr.c_str());

获得不同类型的数据值  

通过数组下标操作符[],根据key获得Value,并使用Value的GetXXX方法获得不同类型的数据值。 

//获取JSON中数组的方法(宠物数组)[dog,cat]
const rapidjson::Value & v=d1["pets"];
for(unsigned int i=0;i<v.Size();++i){
const rapidjson::Value & val=v[i];
log("%s",val.GetString());
}
//根据key获得value(学生信息)"stuInfo":{"stuAge":23,"stuName":"rose","birthday":"1990-01-12"}
const rapidjson::Value & v2=d1["stuInfo"];
//获得整型值
const rapidjson::Value&val1 = v2["stuAge"];
log("val.GetString()=%d",vall.GetInt());
//获得字符串值
const rapidjson::Value&val2 = v2["stuName"];
log("val.GetString()=%s",val2.GetString());
//获得字符串值
const rapidjson::Value&val3 = v2["birthday"];
log("val.GetString()=%s",val3.GetString());
//根据key获得value(other)"other":[true,30]
const rapidjson::Value&v3=d1["other"];
for(unsigned int i=0;i<v3.Size();++i){
    const rapidjson::Value&val=v3[i];
    if(val.IsBool()){
    log("%d",val.GetBool());
    }
    if(val.IsInt()){
    log("%d",val.GetInt());
    } 
}

Atas ialah kandungan terperinci 什么是json数据解析. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:bi是什么?Artikel seterusnya:镜像文件怎么用?