Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to encapsulate and parse data in JSON

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how JSON encapsulates data and parses data". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "how JSON encapsulates data and parses data"!

Introduction to JSON

JSON(JavaScript Object Notation) is a lightweight data exchange format based on a subset of JavaScript that is easy to write and read by humans and easy to parse by machines. JSON uses a completely language-independent text format, but also uses conventions similar to those of the C family (including C, C++, C#, Java, JavaScript, Perl, Python, etc.). These features make JSON an ideal data exchange language.

JSON consists of two structures:

1. An unordered collection of key-value pairs-an object (or record, structure, dictionary, hash table, keyed list, associative array, etc.)

2. Ordered list of values--arrays

These are common data structures. In fact, most modern computer languages support them in some form. This makes it possible to exchange a data format between programming languages that are also based on these structures.

Forms in JSON:

An object is a collection of unordered key-value pairs, starting with "{" and ending with "}," separated by ":" between key-value pairs, and separated by "," between different key-value pairs, for example

{ "key1" : 1, "key2" : "string"}J SON Advantages and disadvantages

Summary:

1. Small bandwidth (compressed format)

2. js reads Json via eval()(easy for client to read)

3. JSON supports multiple languages (c, c++, PHP, etc.) for easy server-side parsing

JSON (JavaScript Object Notation) is a lightweight data interchange format that replaces XML.

Pros:

1. The data format is relatively simple, easy to read and write, the format is compressed, and the bandwidth is small.

2. Easy to parse this language, client javascript can simply read JSON data via eval()

3. Support multiple languages, including ActionScript, C, C#, ColdFusion, Java, JavaScript, Perl, php, Python, Ruby and other server-side languages for server-side parsing

4. In the PHP world, PHP-JSON and JSON-PHP are already available for direct invocation by programs that serialize PHP. PHP server-side objects, arrays, etc. can be directly generated JSON format, easy to access the client extraction. In addition, PHP's PEAR class has been proposed for support (pear.php.net/pepr/pepr-proposal-show.php? id=198)

5. Because the JSON format can be used directly for server-side code, it greatly simplifies the amount of code development on both the server and client sides, but the tasks performed are the same and easy to maintain.

Disadvantages:

1. No XML format is so popular and widely used, no XML is so versatile 2. The JSON format is currently in the early stages of promotion in Web Services PS: It is said that Google's Ajax is done using JSON+ templates

Actual usage of JSON: In the actual call interface, json is often used to transmit data. For example, when requesting the SMS sending interface of Tencent Cloud (Cloud), the request parameter data format is json, as shown in the following figure:

How do you determine if the JSON format is correct in practice? Baidu JSON online tool, direct verification, as shown below:

老铁,这个json格式,没毛病,提示是不是很直接。。。。。。。哈哈。

C++ 使用Json封装数据和解析数据

使用C++和别的语言做交互比较常用的一种数据操作方式就是json。可以到GitHub上下载对应C++的json库源码,我自己下了一套之前的版本,可以正常使用。jsoncpp-src

编译出来的库名:json_vc71_libmtd.lib(debug) | json_vc71_libmt.lib(release)

需要包含的头文件:jsoncpp-src(对应源码)/include/json (该目录下所有文件)

调用库方式#include "json/json.h"#ifdef _DEBUG#pragma comment(lib,"./lib/json_vc71_libmtd.lib")#else#pragma comment(lib,"./lib/json_vc71_libmt.lib")#endif封装json数据为string方法1:std::string DataToJson(){ Json::FastWriter writerinfo; Json::Value writevalueinfo; writevalueinfo["id"]=abc123; writevalueinfo["time"]="2020.04.26 00:00:00";

Json::Value writedata; writedata["count"] = 1; writedata["name"] = "cpp";

writevalueinfo["data"]=writedata;

std::string strEmail = writerinfo.write(writevalueinfo); return strEmail;}示例json://公众号:C与C语言plus{ "data": { "count": 1, "name": "cpp" }, "id": abc123, "time": "2020.04.26 00:00:00"}

方法2(直接拼接):

CString strTempData;msg_content = " 1号设备水位超过阈值报警";cstrsha256 = "20823c7cf53e97898f39f212b9dbee960220b3fa272c884c62f1f3fe6af56f38";strmobile = " 13004091102";strtime = "1587711951 ";strTempData.Format("{\"tpl_id\": %ld,\"params\":\[ \"%s\"\],\"playtimes\": %d,\"sig\": \"%s\",\"tel\":\{\"mobile\":\"%s\",\"nationcode\":\"%d\"\},\"time\": %ld, \"ext\":\"\"\}",568369,msg_content,2,cstrsha256,strmobile,86,strtime);示例json: //公众号:C与C语言plus{ "tpl_id": 568369, "params": ["1号设备水位超过阈值报警 "], "playtimes": 2, "sig": "20823c7cf53e97898f39f212b9dbee960220b3fa272c884c62f1f3fe6af56f38", "tel": { "mobile": "13004091102", "nationcode": "86" }, "time": 1587711951, "ext": ""}解析json数据void TranslateJson(const string strData){ // 解析json用Json::Reader Json::Reader *readerinfo = new Json::Reader(Json::Features::strictMode()); // Json::Value是一种很重要的类型,可以代表任意类型。如int, string, object, array... Json::Value root; if (readerinfo->parse(strData, root)) { if (root["id"].isInt()) { int nID = root["id"].asInt(); } if (root["time"].isString()) { std::string strTime = root["time"].asString(); } if (root["data"]["count"].isInt()) { int nDataCount = root["data"]["count"].asInt(); }

if (root["data"]["name"].isString()) { std::string strDataName = root["data"]["name"].asString(); }

} ::delete readerinfo; readerinfo = NULL;}到此,相信大家对"JSON怎么封装数据和解析数据"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report