2013-01-23 10 views
10

voglio essere in grado di creare il seguente output JSON utilizzando rapidJSONrapidJSON l'aggiunta di una serie di struct

{ 
    "year": 2013, 
    "league": "national", 
    "teams": [ 
     { 
      "teamname": "reds", 
      "teamcity": "cincinnati", 
      "roster": [ 
       { 
        "playername": "john", 
        "position": "catcher" 
       }, 
       { 
        "playername": "joe", 
        "position": "pitcher" 
       } 
      ] 
     } 
    ] 
} 

Questo è valido JSON ... verificati presso JSONLint.com so come creare un documento e usa AddMember per aggiungere "year" e "league".

io non riesco a capire e non ho visto alcun esempio di come aggiungere una matrice che ha una struttura come "squadre" o "roster"

Come si fa a aggiungere "squadre", che è un array di strutture ? Qualsiasi aiuto o indicarmi un esempio sarebbe fantastico.

+0

avrebbe dovuto accettare la risposta – warunanc

risposta

42

Supponiamo di disporre di un roster std :: vector contenente le funzioni di accesso PlayerName() e Postion() nella classe Roster che restituiscono std :: string &.

rapidjson::Document jsonDoc; 
jsonDoc.SetObject(); 
rapidjson::Value myArray(rapidjson::kArrayType); 
rapidjson::Document::AllocatorType& allocator = jsonDoc.GetAllocator(); 

std::vector<Roster*>::iterator iter = roster.begin(); 
std::vector<Roster*>::iterator eiter = roster.end(); 
for (; iter != eiter; ++iter) 
{ 
    rapidjson::Value objValue; 
    objValue.SetObject(); 
    objValue.AddMember("playername", (*iter)->PlayerName().c_str(), allocator); 
    objValue.AddMember("position", (*iter)->Position().c_str(), allocator); 

    myArray.PushBack(objValue, allocator); 
} 

jsonDoc.AddMember("array", myArray, allocator); 
rapidjson::StringBuffer strbuf; 
rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf); 
jsonDoc.Accept(writer); 

const char *jsonString = strbuf.GetString(); // to examine the encoding... 

Questo ti porterà una serie di strutture nel documento. Tutto quello che dovresti fare per ottenere il resto della struttura è annidare gli oggetti quickjson l'uno dentro l'altro e usare AddMember() per costruire il tuo complesso albero degli oggetti. Spero che questo ti aiuti.

+2

Nice one! Purtroppo l'utente non ha accettato la tua risposta, comunque hai fatto la mia giornata! :) – Zappescu

+0

Perfetto risparmiato molto il mio tempo. il dovrebbe avere accettato la tua risposta –

+0

Questo è un esempio a tutto tondo da cui estrarre. –

10

In Vs2012/Rapidjson versione 0.1, la seguente dichiarazione diventa codice non leggibile quando viene stampato il documento da StringBuffer.

objValue.AddMember("position", (*iter)->Position().c_str(), allocator); 

dopo poche ore di dig, ho capito come farlo in modo corretto.

Value tmp; 
tmp.SetString((*iter)->Position().c_str(), allocator); 
objValue.AddMember("position", tmp, allocator); 

ecco un tutorial: http://miloyip.github.io/rapidjson/md_doc_tutorial.html

1
rapidjson::StringBuffer s; 
    rapidjson::Writer<rapidjson::StringBuffer> writer(s); 

    writer.StartObject();    

    std::string year("year"); 
    writer.String(year.c_str(), static_cast<rapidjson::SizeType>(year.length())); 
    writer.Int(2013); 

    std::string league("league"); 
    writer.String(league.c_str(), static_cast<rapidjson::SizeType>(league.length())); 
    std::string national("national"); 
    writer.String(national.c_str(), static_cast<rapidjson::SizeType>(national.length())); 


    std::string teams("teams"); 
    writer.String(teams.c_str(), static_cast<rapidjson::SizeType>(teams.length())); 

    writer.StartArray(); 
    writer.StartObject(); 

    std::string teamname("teamname"); 
    writer.String(teamname.c_str(), static_cast<rapidjson::SizeType>(teamname.length())); 
    std::string reds("reds"); 
    writer.String(reds.c_str(), static_cast<rapidjson::SizeType>(reds.length())); 

    std::string teamcity("teamcity"); 
    writer.String(teamcity.c_str(), static_cast<rapidjson::SizeType>(teamcity.length())); 
    std::string cincinnati("cincinnati"); 
    writer.String(cincinnati.c_str(), static_cast<rapidjson::SizeType>(cincinnati.length())); 

    std::string roster("roster"); 
    writer.String(roster.c_str(), static_cast<rapidjson::SizeType>(roster.length())); 

    writer.StartArray(); 

    writer.StartObject(); 
    std::string playername("playername"); 
    writer.String(playername.c_str(), static_cast<rapidjson::SizeType>(playername.length())); 
    std::string john("john"); 
    writer.String(john.c_str(), static_cast<rapidjson::SizeType>(john.length())); 

    std::string position("position"); 
    writer.String(position.c_str(), static_cast<rapidjson::SizeType>(position.length())); 
    std::string catcher("catcher"); 
    writer.String(catcher.c_str(), static_cast<rapidjson::SizeType>(catcher.length())); 
    writer.EndObject(); 

    // can add one more object in the same manner 
    // with player name and position 

    writer.EndArray(); 

    writer.EndObject(); 

    writer.EndArray(); 
    writer.EndObject(); 


s.GetString();