viggox / rapidjson

Automatically exported from code.google.com/p/rapidjson
MIT License
0 stars 0 forks source link

Not able to convert json string into json object #88

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hi,

Thanks for providing a convenient solution.

I was trying to make a sample program where JSON string is different and the 
result is some strange charaters.

Steps followed: (Using Visual studio 2010 professional)
1) Copied Include folder in my project.
2) Change Json string as:
const char json[] ="{ \"address-book\" : [{ \"name\":\"Jane 
Roe\",\"email\":\"jane.roe@example.com\"},{\"name\":\"John\",\"email\" : 
\"john.doe@example.com\"}]}";
3) Code is compiling successfully in Release mode

4) While debugging:
     a) Pasring is done successfully
     b) But it returns some strange characters at the end.

Please find the attached code for more details.

Thanks and Regards,

Vijay Soni
Software Engineer (c++)

Original issue reported on code.google.com by vijayson...@gmail.com on 12 Aug 2013 at 7:09

GoogleCodeExporter commented 8 years ago
Please refer to the new file attached 

Original comment by vijayson...@gmail.com on 12 Aug 2013 at 7:14

Attachments:

GoogleCodeExporter commented 8 years ago
Your attached code should assert in debug mode, as objJson does not contain a 
key called "name".

Original comment by milo...@gmail.com on 12 Aug 2013 at 8:47

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Hi 

could you please give hint how to traverse through this json string
    "[                                              \
                            {                                           \
                            \"ResponseName\":\"Table\",                 \
                            \"ListData\":                               \
                                [                                       \
                                    {\"Param\":\"Power\"},              \
                                    {\"Param\":\"Def\"},                \
                                    {\"Param\":\"Opt\"},                \
                                ]                                       \
                            }                                           \
                        ]";

As this is an array , but I not able to get its member.

Please help

Original comment by vijayson...@gmail.com on 14 Aug 2013 at 10:23

GoogleCodeExporter commented 8 years ago
Please check "Querying Array" in 
https://code.google.com/p/rapidjson/wiki/UserGuide#3._Value

Original comment by milo...@gmail.com on 14 Aug 2013 at 1:23

GoogleCodeExporter commented 8 years ago
Hi,

 Sorry to trouble you again and again. But using the same I am getting strange reponse that's why I have posted my query here. 

Code is :

#include "stdafx.h"
#include "URI.h"
#include <iostream>
#include <cstdio>

#include "rapidjson\document.h"     // rapidjson's DOM-style API
#include "rapidjson\prettywriter.h" // for stringify JSON
#include "rapidjson\filestream.h"   // wrapper of C stream for prettywriter as 
output
#include "rapidjson\stringbuffer.h"
#include <cstdio>

using namespace std;
using namespace openutils;
using namespace rapidjson;

rapidjson::Document objJson;    // Default template parameter uses UTF8 and 
MemoryPoolAllocator.
rapidjson::StringBuffer strBuff(0, 1024);
rapidjson::Document::AllocatorType& allocator = objJson.GetAllocator();
rapidjson::Value objVal(rapidjson::kObjectType);    

int main(int argc, char *argv[])
{
static const char* kTypeNames[] = { "Null", "False", "True", "Object", "Array", 
"String", "Number" }; 

    const char json[] = "[              \
    {               \
    \"ResponseName\":\"Table\",                 \
    \"ListData\":       \
        [   \                                   {\"Param\":\"Power\"},              \           {\"Param\":\"Def\"},                \
    {\"Param\":\"Opt\"}                 \
]                                   \
}                                   \
]";

    if(objJson.Parse<0>(json).HasParseError())
    {
    printf("Error parsing ");
    }

const Value& a = objJson; // Using a reference for consecutive
assert(a.IsArray());
for (SizeType i = 0; i < a.Size(); i++) // rapidjson uses SizeType instead of 
size_t.         
printf("a[%d] = %d  /n", i, a[i].GetInt());

printf("%s  /n", a[SizeType(0)].GetString());

if(objJson.IsArray())
{                               
for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr)    
printf("%d \n", itr->GetInt());

for (Value::ConstMemberIterator itr = objJson.MemberBegin(); itr != 
objJson.MemberEnd(); ++itr)     
printf("Type of member %s is %s\n", itr->name.GetString(), 
kTypeNames[itr->value.GetType()]);                                  
                }           
        }
        catch(...)
        {
            printf("Error unknown");
        }       
    getchar();
    return 0;
}

==> The response which I get is different everytime. such as (check the 
attachment)

Original comment by vijayson...@gmail.com on 16 Aug 2013 at 6:37

Attachments:

GoogleCodeExporter commented 8 years ago
Would you think that you should use 0L instead ? to remove all chances this is 
ambiguous?

Original comment by thege...@gmail.com on 18 Jun 2014 at 10:36

GoogleCodeExporter commented 8 years ago

Original comment by milo...@gmail.com on 30 Jun 2014 at 1:57