Code Monkey home page Code Monkey logo

simplejson's Introduction

SimpeJSON(sJSON)

简介

SimpleJSon(简称 sJSON)是一个只有一个 hpp 文件的轻量级 C++ JSON 库,使用方法优雅,支持 C11 及以上的 C++ 标准。

实例

下面是一个使用 sJSON 解析 JSON 的实例:

#include "sJson.hpp"

int main()
{
    sJSONParser Parser(R"({
    "sites": [
        { "name":"codeforce" , "url":"www.codeforces.com", "rank":3 }, 
        { "name":"google" , "url":"www.google.com", "rank":2 }, 
        { "name":"github" , "url":"www.github.com", "rank":1 }
    ]
})");
    // 解析 JSON
    auto Root = Parser.Parse();

    // 序列化 JSON 对象
    printf("JSON in format : %s\n", sJSONWriter::WriteJSON(Root).c_str());
    printf("JSON in not format : %s\n\n", sJSONWriter::WriteJSON(Root, false).c_str());

    // 读取 JSON 文件
    for (auto Object = Root["sites"].ArrayBegin(); Object != Root["sites"].ArrayEnd(); ++Object)
    {
        auto ObjectExpand = sJSONElementFinder((*Object)->To<sJSONObject*>());
        for (auto Instance : ObjectExpand)
        {
            printf("<%s, ", Instance.first.c_str());
            if (sJSONString::Equal(**Instance.second)) {
                printf("%s> ", (**Instance.second->To<sJSONString*>()).c_str());
            }
            if (sJSONInt::Equal(**Instance.second)) {
                printf("%d> ", **Instance.second->To<sJSONInt*>());
            }
        }
        printf("\n");
    }

    return 0;
}

运行上面的代码,最终效果如下:

JSON in format : {
        "sites":[
                {
                        "name":"codeforce",
                        "rank":3,
                        "url":"www.codeforces.com"
                },
                {
                        "name":"google",
                        "rank":2,
                        "url":"www.google.com"
                },
                {
                        "name":"github",
                        "rank":1,
                        "url":"www.github.com"
                }
        ]
}
JSON in not format : {"sites":[{"name":"codeforce","rank":3,"url":"www.codeforces.com"},{"name":"google","rank":2,"url":"www.google.com"},{"name":"github","rank":1,"url":"www.github.com"}]}

<name, codeforce> <rank, 3> <url, www.codeforces.com>
<name, google> <rank, 2> <url, www.google.com>
<name, github> <rank, 1> <url, www.github.com>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.