使用rapidjson难度很大.11 GetInt(),总是返回0

时间:2013-05-19 02:14:52

标签: rapidjson

这是json形式:

{"simpleChannels":{"item":[{"channelID":4248,"majorChannelNumber":22,"minorChannelNumber":0,"channelType":"SLL","simpleSchedules":[],"channelKey":"4248_1343210400000","shortName":"KWHY","longName":"A3 Los Angeles 22 KWHY IND","networkAffiliation":["Independent"],"logoID":0,"authCode":"FS","engChlFlag":false,"hasMirror":true,"pgSource":"ACTIVE","hdChlFlag":true,"adultChlFlag":false,"vodProviderId":0,"blackOut":false,"liveStreaming":"N"}]}}

以下是尝试解析和提取值的代码:
json是一个包含上述内容的std :: string:

    m_guideSlice.Parse<0>(json.c_str());
    rapidjson::Value& simpleChannels = m_guideSlice["simpleChannels"];
    if (simpleChannels.IsObject()) {
        SYSLOG_CRITICAL("simpleChannels.IsObject() == true\n");
        rapidjson::Value& channelArray= simpleChannels["item"];
        if (channelArray.IsArray()) {
            SYSLOG_CRITICAL("channelArray.IsArray() == true\n");
            rapidjson::Value& channel = channelArray[0u];
            if (channel.IsObject()) {
                SYSLOG_CRITICAL("channel.isObject() == true\n");
                rapidjson::Value& channelID = channel["channelID"];
                if (channelID.IsInt()) {
                    SYSLOG_CRITICAL("channelID.IsInt() == true, channelID= %d\n", channelID.GetInt());
                }
                else {
                    SYSLOG_CRITICAL("channelID.IsInt() == false, type= %X\n", channelID.GetType());                     
                }
                rapidjson::Value& majorChannelNumber = channel["majorChannelNumber"];
                if (majorChannelNumber.IsInt()) {
                    SYSLOG_CRITICAL("majorChannelNumber.IsInt() == true, majorChannelNumber= %d\n", majorChannelNumber.GetInt());
                }
                else {
                    SYSLOG_CRITICAL("majorChannelNumber.IsInt() == false, type= %X\n", majorChannelNumber.GetType());                       
                }
                rapidjson::Value& channelType = channel["channelType"];
                if (channelType.IsString()) {
                    SYSLOG_CRITICAL("channelType.IsString() == true\n")
                    SYSLOG_CRITICAL("channelType = %s\n", channelType.GetString());
                }
                else {
                    SYSLOG_CRITICAL("channelType.IsString() == false, type= %X\n", channelType.GetType());
                }
                rapidjson::Value& shortName = channel["shortName"];
                if (channelType.IsString()) {
                    SYSLOG_CRITICAL("shortName.IsString() == true\n")
                    SYSLOG_CRITICAL("shortName = %s\n", shortName.GetString());
                }
                else {
                    SYSLOG_CRITICAL("shortName.IsString() == false, type= %X\n", shortName.GetType());
                }
            }
            else {
                SYSLOG_CRITICAL("channel.IsObject() == false, type= %X\n", channel.GetType());
            }
        }
        else {
            SYSLOG_CRITICAL("channelArray.IsArray() == false, type= %X\n", channelArray.GetType());             
        }
    }
    else {
        SYSLOG_CRITICAL("simpleChannels.IsObject() == false, type= %X\n", simpleChannels.GetType());
    }

以下是syslog提取的输出:

May 19 01:25:38   [BSP] [30578]: [PGWS-GEN LOG_CRIT tid:30744 pthr:0x7033e4f0]:   (PgwsIngest.cpp:78) getGuide(): simpleChannels.IsObject() == true 
May 19 01:25:38   [BSP] [30578]: [PGWS-GEN LOG_CRIT tid:30744 pthr:0x7033e4f0]: (PgwsIngest.cpp:81) getGuide(): channelArray.IsArray() == true 
May 19 01:25:38   [BSP] [30578]: [PGWS-GEN LOG_CRIT tid:30744 pthr:0x7033e4f0]: (PgwsIngest.cpp:84) getGuide(): channel.isObject() == true 
May 19 01:25:38   [BSP] [30578]: [PGWS-GEN LOG_CRIT tid:30744 pthr:0x7033e4f0]: (PgwsIngest.cpp:87) getGuide(): channelID.IsInt() == true, channelID= 0 
May 19 01:25:38   [BSP] [30578]: [PGWS-GEN LOG_CRIT tid:30744 pthr:0x7033e4f0]: (PgwsIngest.cpp:94) getGuide(): majorChannelNumber.IsInt() == true, majorChannelNumber= 0 
May 19 01:25:38   [BSP] [30578]: [PGWS-GEN LOG_CRIT tid:30744 pthr:0x7033e4f0]: (PgwsIngest.cpp:101) getGuide(): channelType.IsString() == true 
May 19 01:25:38   [BSP] [30578]: [PGWS-GEN LOG_CRIT tid:30744 pthr:0x7033e4f0]: (PgwsIngest.cpp:102) getGuide(): channelType = SLL 
May 19 01:25:38   [BSP] [30578]: [PGWS-GEN LOG_CRIT tid:30744 pthr:0x7033e4f0]: (PgwsIngest.cpp:109) getGuide(): shortName.IsString() == true 
May 19 01:25:38   [BSP] [30578]: [PGWS-GEN LOG_CRIT tid:30744 pthr:0x7033e4f0]: (PgwsIngest.cpp:110) getGuide(): shortName = KWHY   

char json值被正确提取但是int json值总是0.我是第一次使用rapidjson用户所以我确定我正在制作一个简单的错误但是我没有看到它就马上了。

全心全意,

顺便说一句,json表单传递http://www.freeformatter.com/json-validator.html所以我认为这是正确的。无论如何它都是机器生成的。

2 个答案:

答案 0 :(得分:1)

我在大端机器上,编译器不能传递适当的标志来表示字节顺序 明确设置标志:

定义RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN

在rapidjon.h中的

应该解决这个问题。

答案 1 :(得分:0)

我们已在此pull request中改进了字节序检测。

它将检测更多平台定义的宏。如果无法成功检测到,则会生成预处理器错误,然后您必须定义RAPIDJSON_ENDIAN

您可以将此差异合并到您的版本中以查看它是否有帮助。