sto scrivendo del codice in C++ ed ho la necessita di leggere alcuni dati da elaborare che sono all'interno di un file .json.
Come ambiente di sviluppo sto usando Eclipse su Debian e ho installato la libreria jsoncpp.
Non ho la più pallida idea di come si possa usare un json in C++. In rete ho trovato mille guide che descrivono sintassi diverse tra loro, ma nessuna mi ha portato ad un risultato seppur minimo. Con i dati in questo file dovrei fare delle elaborazioni anche articolate, ma non sono nemmeno riuscito a stampare a video il suo contenuto...
Questo è il file orbitals.json
- Codice: Seleziona tutto
{
"orbitals":{
"levels" :[
{
"level":1,
"name":"1s",
"n_value" : 1,
"l_value" : 0,
"degener" : 2
},
{
"level":2,
"name":"1p",
"n_value" : 1,
"l_value" : 1,
"degener" : 6
},
{
"level":3,
"name":"1d",
"n_value" : 1,
"l_value" : 2,
"degener" : 10
},
{
"level":4,
"name":"2s",
"n_value" : 2,
"l_value" : 0,
"degener" : 2
},
{
"level":5,
"bame":"1f",
"n_value" : 1,
"l_value" : 3,
"degener" : 14
},
{
"level":6,
"name":"2p",
"n_value" : 2,
"l_value" : 1,
"degener" : 6
},
{
"level":7,
"name":"1g",
"n_value" : 1,
"l_value" : 4,
"degener" : 18
},
{
"level":8,
"name":"2d",
"n_value" : 2,
"l_value" : 2,
"degener" : 10
},
{
"level":9,
"name":"3s",
"n_value" : 3,
"l_value" : 0,
"degener" : 2
},
{
"level":10,
"name":"1h",
"n_value" : 1,
"l_value" : 5,
"degener" : 22
}
]
}
}
e questa sarebbe la parte di codice C++ che dovrebbe gestire il json:
- Codice: Seleziona tutto
#include <iostream>
#include <fstream>
#include <vector>
#include <jsoncpp/json/json.h>
#include <jsoncpp/json/reader.h>
#include <jsoncpp/json/writer.h>
#include <jsoncpp/json/value.h>
#include "Includes.h"
int main(int argc, const char * argv[]) {
Json::Value root;
Json::Reader reader;
std::ifstream ifs ("orbitals3.json"); // this read
ifs >> root; // the entire file
const Json::Value array = root["orbitals"]["levels"]["level"];
for (int i=0; i<array.size(); ++i)
{
std::cout << array[i].asInt() << std::endl;
}
return 0;
}
Qualcuno potrebbe spiegarmi dove sbaglio e che sintassi devo usare?
Grazie mille!