您的位置:首页 > Web前端 > JavaScript

windows客户端开发--使用json11解析json

2016-03-28 22:41 686 查看


类似xml,JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。

json11是适用于c++11的一个轻量级的库,用于解析json.

什么是json?

JSON is short for JavaScript Object Notation, and is a way to store information in an organized, easy-to-access manner. In a nutshell, it gives us a human-readable collection of data that we can access in a really logical manner.

json如何存储?

var jason = {
"age" : "24",
"hometown" : "Missoula, MT",
"gender" : "male"
};


将json存为数组

var family = [{
"name" : "Jason",
"age" : "24",
"gender" : "male"
},
{
"name" : "Kyle",
"age" : "21",
"gender" : "male"
}];


使用json11进行解析

#include <string.h>
#include <cstdio>
#include <iostream>
#include <sstream>
#include "lasote/json11/json11.hpp"
#include <cassert>
#include <list>
#include <set>
#include <unordered_map>

using namespace json11;
using std::string;

int main(int argc, char **argv) {

// STRING TO JSON
const string simple_test =
R"({"k1":"v1", "k2":42, "k3":["a",123,true,false,null]})";

string err;
auto json = Json::parse(simple_test, err);
std::cout << "k1: " << json["k1"].string_value() << "\n";
std::cout << "k3: " << json["k3"].dump() << "\n";

// JSON FROM LITERAL
Json obj = Json::object({
{ "k1", "v1" },
{ "k2", 42.0 },
{ "k3", Json::array({ "a", 123.0, true, false, nullptr }) },
});

std::cout << "obj: " << obj.dump() << "\n";

// CUSTOM CLASS JSON ENCODE
class Point {
public:
int x;
int y;
Point (int x, int y) : x(x), y(y) {}
Json to_json() const { return Json::array { x, y }; }
};

std::vector<Point> points = { { 1, 2 }, { 10, 20 }, { 100, 200 } };
std::string points_json = Json(points).dump();
printf("%s\n", points_json.c_str());
}


josn11源码头文件

class __declspec(dllexport) Json final {
public:
// Types
enum Type {
NUL, NUMBER, BOOL, STRING, ARRAY, OBJECT
};

// Array and object typedefs
typedef std::vector<Json> array;
typedef std::map<std::string, Json> object;

// Constructors for the various types of JSON value.
Json() noexcept;                // NUL
Json(std::nullptr_t) noexcept;  // NUL
Json(double value);             // NUMBER
Json(int value);                // NUMBER
Json(bool value);               // BOOL
Json(const std::string &value); // STRING
Json(std::string &&value);      // STRING
Json(const char * value);       // STRING
Json(const array &values);      // ARRAY
Json(array &&values);           // ARRAY
Json(const object &values);     // OBJECT
Json(object &&values);          // OBJECT

// Implicit constructor: anything with a to_json() function.
template <class T, class = decltype(&T::to_json)>
Json(const T & t) : Json(t.to_json()) {}

// Implicit constructor: map-like objects (std::map, std::unordered_map, etc)
template <class M, typename std::enable_if<
std::is_constructible<std::string, typename M::key_type>::value
&& std::is_constructible<Json, typename M::mapped_type>::value,
int>::type = 0>
Json(const M & m) : Json(object(m.begin(), m.end())) {}

// Implicit constructor: vector-like objects (std::list, std::vector, std::set, etc)
template <class V, typename std::enable_if<
std::is_constructible<Json, typename V::value_type>::value,
int>::type = 0>
Json(const V & v) : Json(array(v.begin(), v.end())) {}

// This prevents Json(some_pointer) from accidentally producing a bool. Use
// Json(bool(some_pointer)) if that behavior is desired.
Json(void *) = delete;

// Accessors
Type type() const;

bool is_null()   const { return type() == NUL; }
bool is_number() const { return type() == NUMBER; }
bool is_bool()   const { return type() == BOOL; }
bool is_string() const { return type() == STRING; }
bool is_array()  const { return type() == ARRAY; }
bool is_object() const { return type() == OBJECT; }

// Return the enclosed value if this is a number, 0 otherwise. Note that json11 does not
// distinguish between integer and non-integer numbers - number_value() and int_value()
// can both be applied to a NUMBER-typed object.
double number_value() const;
int int_value() const;

// Return the enclosed value if this is a boolean, false otherwise.
bool bool_value() const;
// Return the enclosed string if this is a string, "" otherwise.
const std::string &string_value() const;
// Return the enclosed std::vector if this is an array, or an empty vector otherwise.
const array &array_items() const;
// Return the enclosed std::map if this is an object, or an empty map otherwise.
const object &object_items() const;

// Return a reference to arr[i] if this is an array, Json() otherwise.
const Json & operator[](size_t i) const;
// Return a reference to obj[key] if this is an object, Json() otherwise.
const Json & operator[](const std::string &key) const;

// Serialize.
void dump(std::string &out) const;
std::string dump() const {
std::string out;
dump(out);
return out;
}

// Parse. If parse fails, return Json() and assign an error message to err.
static Json parse(const std::string & in, std::string & err);
static Json parse(const char * in, std::string & err) {
if (in) {
return parse(std::string(in), err);
} else {
err = "null input";
return nullptr;
}
}
// Parse multiple objects, concatenated or separated by whitespace
static std::vector<Json> parse_multi(const std::string & in, std::string & err);

bool operator== (const Json &rhs) const;
bool operator<  (const Json &rhs) const;
bool operator!= (const Json &rhs) const { return !(*this == rhs); }
bool operator<= (const Json &rhs) const { return !(rhs < *this); }
bool operator>  (const Json &rhs) const { return  (rhs < *this); }
bool operator>= (const Json &rhs) const { return !(*this < rhs); }

/* has_shape(types, err)
*
* Return true if this is a JSON object and, for each item in types, has a field of
* the given type. If not, return false and set err to a descriptive message.
*/
typedef std::initializer_list<std::pair<std::string, Type>> shape;
bool has_shape(const shape & types, std::string & err) const;

private:
std::shared_ptr<JsonValue> m_ptr;
};

// Internal class hierarchy - JsonValue objects are not exposed to users of this API.
class JsonValue {
protected:
friend class Json;
friend class JsonInt;
friend class JsonDouble;
virtual Json::Type type() const = 0;
virtual bool equals(const JsonValue * other) const = 0;
virtual bool less(const JsonValue * other) const = 0;
virtual void dump(std::string &out) const = 0;
virtual double number_value() const;
virtual int int_value() const;
virtual bool bool_value() const;
virtual const std::string &string_value() const;
virtual const Json::array &array_items() const;
virtual const Json &operator[](size_t i) const;
virtual const Json::object &object_items() const;
virtual const Json &operator[](const std::string &key) const;
virtual ~JsonValue() {}
};

} // namespace json11


哦对了,写到现在连个windows还没提到呢?难道博主是挂鸡头卖鸭头?

错。在客户端开发中,pc也好 移动也好,总是需要服务器返回的数据。

这个response往往就是json类型的,所以我们做客户端的需要对json对象进行解析,然后进行使用~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: