本文介绍了Json序列化(编组和解组)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些示例,说明如何从Json格式序列化/反序列化到Object以及从Object序列化/反序列化.我使用了以下示例http://www.boost.org/doc/libs/1_47_0/libs/serialization/doc/index.html,但它仅序列化为简单文本.在我的项目中,出于其他要求,我正在成功使用Boost库.我几乎可以确定我确实需要其他一些特定的库,例如Json-Spirit或标准QT库(QScriptEngine).任何简单的示例将不胜感激.

这是我必须序列化为类的Json文件的示例.

I need some example of how to serialize/deserilize from Json format to Object and from Object to Json. I used this example http://www.boost.org/doc/libs/1_47_0/libs/serialization/doc/index.html, but it only serialize to a simple text. In my project, for other requirenments, I am using boost library with success. I am almost sure that I do need some other specific library like Json-Spirit or standard QT library (QScriptEngine). Any simple example will be very appreciate.

This is an example of the Json files I must serialized to a class.

[{"DscTipoEstrutura":"Unidade de Neg\u00f3cio","FlgAtivo":true,"IdDominio":1,"IdTipoEstrutura":1,"NroOrdem":0},{"DscTipoEstrutura":"Regi\u00e3o","FlgAtivo":true,"IdDominio":1,"IdTipoEstrutura":2,"NroOrdem":1},{"DscTipoEstrutura":"Distrito","FlgAtivo":true,"IdDominio":1,"IdTipoEstrutura":3,"NroOrdem":2}]



附注:我发现http://www.codeproject.com/KB/recipes/JSON_Spirit.aspx和http://boost-spirit.com/repository/applications/show_contents.php与我类似,但因为我是C ++更好的是,我不明白他们几个小时后都无法运行该示例.我需要一些简单的示例.



Ps.: I found http://www.codeproject.com/KB/recipes/JSON_Spirit.aspx and http://boost-spirit.com/repository/applications/show_contents.php similiar to I need but, because I am C++ begnnier, I couldn''t understand them neither run the example after some couple of hours. I need some simple example.

推荐答案

#include "json_spirit.h"
#include <cassert>
#include <fstream>

#ifndef JSON_SPIRIT_VALUE_ENABLED
#error Please define JSON_SPIRIT_VALUE_ENABLED for the Value type to be enabled
#endif

using namespace std;
using namespace json_spirit;

struct Address
{
    string DscTipoEstrutura_;
    bool FlgAtivo_;
    int IdDominio_;
    int IdTipoEstrutura_;
    int NroOrdem_;
};

bool operator==( const Address& a1, const Address& a2 )
{
    return ( a1.DscTipoEstrutura_ == a2.DscTipoEstrutura_ ) &&
           ( a1.FlgAtivo_         == a2.FlgAtivo_ ) &&
           ( a1.IdDominio_         == a2.IdDominio_ ) &&
           ( a1.IdTipoEstrutura_       == a2.IdTipoEstrutura_ ) &&
           ( a1.NroOrdem_      == a2.NroOrdem_ );
}

void write_address( Array& a, const Address& addr )
{
    Object addr_obj;

    addr_obj.push_back( Pair( "DscTipoEstrutura", addr.DscTipoEstrutura_ ) );
    addr_obj.push_back( Pair( "FlgAtivo",         addr.FlgAtivo_ ) );
    addr_obj.push_back( Pair( "IdDominio",         addr.IdDominio_ ) );
    addr_obj.push_back( Pair( "IdTipoEstrutura",       addr.IdTipoEstrutura_ ) );
    addr_obj.push_back( Pair( "NroOrdem",      addr.NroOrdem_ ) );

    a.push_back( addr_obj );
}

Address read_address( const Object& obj )
{
    Address addr;
	string strBool;

    for( Object::size_type i = 0; i != obj.size(); ++i )
    {
        const Pair& pair = obj[i];

        const string& name  = pair.name_;
        const Value&  value = pair.value_;

        if( name == "DscTipoEstrutura" )
        {
            addr.DscTipoEstrutura_ = value.get_str();
        }
        else if( name == "FlgAtivo" )
        {
			addr.FlgAtivo_ = value.get_bool();;//to be changed
        }
        else if( name == "IdDominio" )
        {
            addr.IdDominio_ = value.get_int();
        }
        else if( name == "IdTipoEstrutura" )
        {
            addr.IdTipoEstrutura_ = value.get_int();
        }
        else if( name == "NroOrdem" )
        {
            addr.NroOrdem_ = value.get_int();
        }
        else
        {
            assert( false );
        }
    }

    return addr;
}

void write_addrs( const char* file_name, const Address addrs[] )
{
    Array addr_array;

    for( int i = 0; i < 5; ++i )
    {
        write_address( addr_array, addrs[i] );
    }

    ofstream os( file_name );

    write_formatted( addr_array, os );

    os.close();
}

vector< Address > read_addrs( const char* file_name )
{
    ifstream is( file_name );

    Value value;

    read( is, value );

    const Array& addr_array = value.get_array();

    vector< Address > addrs;

    for( unsigned int i = 0; i < addr_array.size(); ++i )
    {
        addrs.push_back( read_address( addr_array[i].get_obj() ) );
    }

    return addrs;
}

int main()
{
    const Address addrs[5] = { { "Unidade de Neg\u00f3cio",		"true",	   1,	1,	0 },
                               { "Regi\u00e3o",     "true",    1,	2	,1 },
                               { "Distrito", "true",   1, 3,2   },
                               { "jkl", "false",   3,    3, 2 },
                               { "mno", "false", 2, 4, 2 } };

    const char* file_name( "demo.txt" );

    write_addrs( file_name, addrs );//de la représentation tabulée au JSON

    vector< Address > new_addrs = read_addrs( file_name );//du JSON à la représentation tabulée

    assert( new_addrs.size() == 5 );

    for( int i = 0; i < 5; ++i )
    {
        assert( new_addrs[i] == addrs[i] );
    }

	return 0;
}
</fstream></cassert>



希望它能对您有所帮助,或者告诉您解决方案不是您期望的那样.

来自法国,
加布里埃尔



Hope it helps and tell me if you have problems or if the solution is not what you expected.

By from France,
Gabriel


这篇关于Json序列化(编组和解组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 09:05