Çözüldü C++'ta JSON dosyasından değer bulma

Bu konu çözüldü olarak işaretlenmiştir. Çözülmediğini düşünüyorsanız konuyu rapor edebilirsiniz.

Huso112

Hectopat
Katılım
26 Ocak 2021
Mesajlar
1.471
Makaleler
1
Çözümler
2
Merhabalar.
Ben C++'a yeni başladım ve json dosyasında arama yapmak istiyorum.
Mesela:
arg[2] değeri 3 ise JSON dosyasında Atom Number değeri 3 olan veriyi yazdır.

gelgelelim ki Python'da hemencecik yaptığım şeyi burada bir türlü edemedim.
Nasıl yapabilirim?

İşte kod:
C++:
#include <iostream>
#include <fstream>
#include <json/json.h>
#include <json/value.h>

using namespace std;

// struct instead of class
struct Element {
    int atomNumber;
    string atomName;
    string atomSymbol;
    string atomType;
    string atomSerie;

// void funciton instead of Book(){}
    void printinfo() {
        cout << "Atom Number: " << atomNumber << "\n";
        cout << "Atom name: " << atomName << "\n";
        cout << "Atom Symbol: " << atomSymbol << "\n";
        cout << "Atom Type: " << atomType << "\n";
        cout << "Atom Serie: " << atomSerie << "\n";
    }
};

//         Arg Count      Args
//            V            V
int main(int argc, char* argv[]) {
    Json::Value ptable;
    std::ifstream ptable_file("ptable.json", std::ifstream::binary);
    ptable_file >> ptable;

    if (strcmp(argv[1], "-p") == 0){
        cout << "arg[2]\'de ne yazıyorsa onu JSON dosyasındaki atomNum değeri ile eşleşen kaydını yazdır" << endl;
    };

    return 0;
}

Json dosyası:
JSON:
[
  {
    "Atom Number": 1,
    "Name": "Hydrogen",
    "Symbol": "H",
    "Type": "Nonmetal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 2,
    "Name": "Helium",
    "Symbol": "He",
    "Type": "Nonmetal",
    "Serie": "Noble gas"
  },
  {
    "Atom Number": 3,
    "Name": "Lithium",
    "Symbol": "Li",
    "Type": "Metal",
    "Serie": "Alkali metal"
  },
  {
    "Atom Number": 4,
    "Name": "Beryllium",
    "Symbol": "Be",
    "Type": "Metal",
    "Serie": "Alkaline earth metal"
  },
  {
    "Atom Number": 5,
    "Name": "Boron",
    "Symbol": "B",
    "Type": "Metalloid",
    "Serie": "Boron group"
  },
  {
    "Atom Number": 6,
    "Name": "Carbon",
    "Symbol": "C",
    "Type": "Nonmetal",
    "Serie": "Carbon group"
  },
  {
    "Atom Number": 7,
    "Name": "Nitrogen",
    "Symbol": "N",
    "Type": "Nonmetal",
    "Serie": "Pnictogen"
  },
  {
    "Atom Number": 8,
    "Name": "Oxygen",
    "Symbol": "O",
    "Type": "Nonmetal",
    "Serie": "Chalcogen"
  },
  {
    "Atom Number": 9,
    "Name": "Fluorine",
    "Symbol": "F",
    "Type": "Nonmetal",
    "Serie": "Halogen"
  },
  {
    "Atom Number": 10,
    "Name": "Neon",
    "Symbol": "Ne",
    "Type": "Nonmetal",
    "Serie": "Noble gas"
  },
  {
    "Atom Number": 11,
    "Name": "Sodium",
    "Symbol": "Na",
    "Type": "Metal",
    "Serie": "Alkali metal"
  },
  {
    "Atom Number": 12,
    "Name": "Magnesium",
    "Symbol": "Mg",
    "Type": "Metal",
    "Serie": "Alkaline earth metal"
  },
  {
    "Atom Number": 13,
    "Name": "Aluminium",
    "Symbol": "Al",
    "Type": "Metal",
    "Serie": "Boron group  & Post-transition metal"
  },
  {
    "Atom Number": 14,
    "Name": "Silicon",
    "Symbol": "Si",
    "Type": "Metalloid",
    "Serie": "Carbon group"
  },
  {
    "Atom Number": 15,
    "Name": "Phosphorus",
    "Symbol": "P",
    "Type": "Nonmetal",
    "Serie": "Pnictogen"
  },
  {
    "Atom Number": 16,
    "Name": "Sulfur",
    "Symbol": "S",
    "Type": "Nonmetal",
    "Serie": "Chalcogen"
  },
  {
    "Atom Number": 17,
    "Name": "Chlorine",
    "Symbol": "Cl",
    "Type": "Nonmetal",
    "Serie": "Halogen"
  },
  {
    "Atom Number": 18,
    "Name": "Argon",
    "Symbol": "Ar",
    "Type": "Nonmetal",
    "Serie": "Noble gas"
  },
  {
    "Atom Number": 19,
    "Name": "Potassium",
    "Symbol": "K",
    "Type": "Metal",
    "Serie": "Alkali metal"
  },
  {
    "Atom Number": 20,
    "Name": "Calcium",
    "Symbol": "Ca",
    "Type": "Metal",
    "Serie": "Alkaline earth metal"
  },
  {
    "Atom Number": 21,
    "Name": "Scandium",
    "Symbol": "Sc",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 22,
    "Name": "Titanium",
    "Symbol": "Ti",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 23,
    "Name": "Vanadium",
    "Symbol": "V",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 24,
    "Name": "Chromium",
    "Symbol": "Cr",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 25,
    "Name": "Manganese",
    "Symbol": "Mn",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 26,
    "Name": "Iron",
    "Symbol": "Fe",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 27,
    "Name": "Cobalt",
    "Symbol": "Co",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 28,
    "Name": "Nickel",
    "Symbol": "Ni",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 29,
    "Name": "Copper",
    "Symbol": "Cu",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 30,
    "Name": "Zinc",
    "Symbol": "Zn",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 31,
    "Name": "Gallium",
    "Symbol": "Ga",
    "Type": "Metal",
    "Serie": "Boron group  & Post-transition metal"
  },
  {
    "Atom Number": 32,
    "Name": "Germanium",
    "Symbol": "Ge",
    "Type": "Metalloid",
    "Serie": "Carbon group"
  },
  {
    "Atom Number": 33,
    "Name": "Arsenic",
    "Symbol": "As",
    "Type": "Metalloid",
    "Serie": "Pnictogen"
  },
  {
    "Atom Number": 34,
    "Name": "Selenium",
    "Symbol": "Se",
    "Type": "Nonmetal",
    "Serie": "Chalcogen"
  },
  {
    "Atom Number": 35,
    "Name": "Bromine",
    "Symbol": "Br",
    "Type": "Nonmetal",
    "Serie": "Halogen"
  },
  {
    "Atom Number": 36,
    "Name": "Krypton",
    "Symbol": "Kr",
    "Type": "Nonmetal",
    "Serie": "Noble gas"
  },
  {
    "Atom Number": 37,
    "Name": "Rubidium",
    "Symbol": "Rb",
    "Type": "Metal",
    "Serie": "Alkali metal"
  },
  {
    "Atom Number": 38,
    "Name": "Strontium",
    "Symbol": "Sr",
    "Type": "Metal",
    "Serie": "Alkaline earth metal"
  },
  {
    "Atom Number": 39,
    "Name": "Yttrium",
    "Symbol": "Y",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 40,
    "Name": "Zirconium",
    "Symbol": "Zr",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 41,
    "Name": "Niobium",
    "Symbol": "Nb",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 42,
    "Name": "Molybdenum",
    "Symbol": "Mo",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 43,
    "Name": "Technetium",
    "Symbol": "Tc",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 44,
    "Name": "Ruthenium",
    "Symbol": "Ru",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 45,
    "Name": "Rhodium",
    "Symbol": "Rh",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 46,
    "Name": "Palladium",
    "Symbol": "Pd",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 47,
    "Name": "Silver",
    "Symbol": "Ag",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 48,
    "Name": "Cadmium",
    "Symbol": "Cd",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 49,
    "Name": "Indium",
    "Symbol": "In",
    "Type": "Metal",
    "Serie": "Boron group  & Post-transition metal"
  },
  {
    "Atom Number": 50,
    "Name": "Tin",
    "Symbol": "Sn",
    "Type": "Metal",
    "Serie": "Carbon group"
  },
  {
    "Atom Number": 51,
    "Name": "Antimony",
    "Symbol": "Sb",
    "Type": "Metalloid",
    "Serie": "Pnictogen & Post-transition metal"
  },
  {
    "Atom Number": 52,
    "Name": "Tellurium",
    "Symbol": "Te",
    "Type": "Metalloid",
    "Serie": "Chalcogen"
  },
  {
    "Atom Number": 53,
    "Name": "Iodine",
    "Symbol": "I",
    "Type": "Nonmetal",
    "Serie": "Halogen"
  },
  {
    "Atom Number": 54,
    "Name": "Xenon",
    "Symbol": "Xe",
    "Type": "Nonmetal",
    "Serie": "Noble gas"
  },
  {
    "Atom Number": 55,
    "Name": "Caesium",
    "Symbol": "Cs",
    "Type": "Metal",
    "Serie": "Alkali metal"
  },
  {
    "Atom Number": 56,
    "Name": "Barium",
    "Symbol": "Ba",
    "Type": "Metal",
    "Serie": "Alkaline earth metal"
  },
  {
    "Atom Number": 57,
    "Name": "Lanthanum",
    "Symbol": "La",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 58,
    "Name": "Cerium",
    "Symbol": "Ce",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 59,
    "Name": "Praseodymium",
    "Symbol": "Pr",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 60,
    "Name": "Neodymium",
    "Symbol": "Nd",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 61,
    "Name": "Promethium",
    "Symbol": "Pm",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 62,
    "Name": "Samarium",
    "Symbol": "Sm",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 63,
    "Name": "Europium",
    "Symbol": "Eu",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 64,
    "Name": "Gadolinium",
    "Symbol": "Gd",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 65,
    "Name": "Terbium",
    "Symbol": "Tb",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 66,
    "Name": "Dysprosium",
    "Symbol": "Dy",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 67,
    "Name": "Holmium",
    "Symbol": "Ho",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 68,
    "Name": "Erbium",
    "Symbol": "Er",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 69,
    "Name": "Thulium",
    "Symbol": "Tm",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 70,
    "Name": "Ytterbium",
    "Symbol": "Yb",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 71,
    "Name": "Lutetium",
    "Symbol": "Lu",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 72,
    "Name": "Hafnium",
    "Symbol": "Hf",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 73,
    "Name": "Tantalum",
    "Symbol": "Ta",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 74,
    "Name": "Tungsten",
    "Symbol": "W",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 75,
    "Name": "Rhenium",
    "Symbol": "Re",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 76,
    "Name": "Osmium",
    "Symbol": "Os",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 77,
    "Name": "Iridium",
    "Symbol": "Ir",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 78,
    "Name": "Platinum",
    "Symbol": "Pt",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 79,
    "Name": "Gold",
    "Symbol": "Au",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 80,
    "Name": "Mercury",
    "Symbol": "Hg",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 81,
    "Name": "Thallium",
    "Symbol": "Tl",
    "Type": "Metal",
    "Serie": "Boron group  & Post-transition metal"
  },
  {
    "Atom Number": 82,
    "Name": "Lead",
    "Symbol": "Pb",
    "Type": "Metal",
    "Serie": "Carbon group"
  },
  {
    "Atom Number": 83,
    "Name": "Bismuth",
    "Symbol": "Bi",
    "Type": "Metal",
    "Serie": "Pnictogen & Post-transition metal"
  },
  {
    "Atom Number": 84,
    "Name": "Polonium",
    "Symbol": "Po",
    "Type": "Metal",
    "Serie": "Chalcogen  & Post-transition metal"
  },
  {
    "Atom Number": 85,
    "Name": "Astatine",
    "Symbol": "At",
    "Type": "Metalloid",
    "Serie": "Halogen"
  },
  {
    "Atom Number": 86,
    "Name": "Radon",
    "Symbol": "Rn",
    "Type": "Nonmetal",
    "Serie": "Noble gas"
  },
  {
    "Atom Number": 87,
    "Name": "Francium",
    "Symbol": "Fr",
    "Type": "Metal",
    "Serie": "Alkali metal"
  },
  {
    "Atom Number": 88,
    "Name": "Radium",
    "Symbol": "Ra",
    "Type": "Metal",
    "Serie": "Alkaline earth metal"
  },
  {
    "Atom Number": 89,
    "Name": "Actinium",
    "Symbol": "Ac",
    "Type": "Metal",
    "Serie": "Actinide"
  },
  {
    "Atom Number": 90,
    "Name": "Thorium",
    "Symbol": "Th",
    "Type": "Metal",
    "Serie": "Actinide"
  },
  {
    "Atom Number": 91,
    "Name": "Protactinium",
    "Symbol": "Pa",
    "Type": "Metal",
    "Serie": "Actinide"
  },
  {
    "Atom Number": 92,
    "Name": "Uranium",
    "Symbol": "U",
    "Type": "Metal",
    "Serie": "Actinide"
  },
  {
    "Atom Number": 93,
    "Name": "Neptunium",
    "Symbol": "Np",
    "Type": "Metal",
    "Serie": "Actinide"
  },
  {
    "Atom Number": 94,
    "Name": "Plutonium",
    "Symbol": "Pu",
    "Type": "Metal",
    "Serie": "Actinide"
  },
  {
    "Atom Number": 95,
    "Name": "Americium",
    "Symbol": "Am",
    "Type": "Metal",
    "Serie": "Actinide"
  },
  {
    "Atom Number": 96,
    "Name": "Curium",
    "Symbol": "Cm",
    "Type": "Metal",
    "Serie": "Actinide"
  },
  {
    "Atom Number": 97,
    "Name": "Berkelium",
    "Symbol": "Bk",
    "Type": "Metal",
    "Serie": "Actinide"
  },
  {
    "Atom Number": 98,
    "Name": "Californium",
    "Symbol": "Cf",
    "Type": "Metal",
    "Serie": "Actinide"
  },
  {
    "Atom Number": 99,
    "Name": "Einsteinium",
    "Symbol": "Es",
    "Type": "Metal",
    "Serie": "Actinide"
  },
  {
    "Atom Number": 100,
    "Name": "Fermium",
    "Symbol": "Fm",
    "Type": "Metal",
    "Serie": "Actinide"
  },
  {
    "Atom Number": 101,
    "Name": "Mendelevium",
    "Symbol": "Md",
    "Type": "Metal",
    "Serie": "Actinide"
  },
  {
    "Atom Number": 102,
    "Name": "Nobelium",
    "Symbol": "No",
    "Type": "Metal",
    "Serie": "Actinide"
  },
  {
    "Atom Number": 103,
    "Name": "Lawrencium",
    "Symbol": "Lr",
    "Type": "Metal",
    "Serie": "Actinide"
  },
  {
    "Atom Number": 104,
    "Name": "Rutherfordium",
    "Symbol": "Rf",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 105,
    "Name": "Dubnium",
    "Symbol": "Db",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 106,
    "Name": "Seaborgium",
    "Symbol": "Sg",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 107,
    "Name": "Bohrium",
    "Symbol": "Bh",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 108,
    "Name": "Hassium",
    "Symbol": "Hs",
    "Type": "Metal",
    "Serie": "Transition metal"
  },
  {
    "Atom Number": 109,
    "Name": "Meitnerium",
    "Symbol": "Mt",
    "Type": "Unknown",
    "Serie": "Unknown"
  },
  {
    "Atom Number": 110,
    "Name": "Darmstadtium",
    "Symbol": "Ds",
    "Type": "Unknown",
    "Serie": "Unknown"
  },
  {
    "Atom Number": 111,
    "Name": "Roentgenium",
    "Symbol": "Rg",
    "Type": "Unknown",
    "Serie": "Unknown"
  },
  {
    "Atom Number": 112,
    "Name": "Copernicium",
    "Symbol": "Cn",
    "Type": "Unknown",
    "Serie": "Unknown"
  },
  {
    "Atom Number": 113,
    "Name": "Nihonium",
    "Symbol": "Nh",
    "Type": "Unknown",
    "Serie": "Unknown"
  },
  {
    "Atom Number": 114,
    "Name": "Flerovium",
    "Symbol": "Fl",
    "Type": "Unknown",
    "Serie": "Unknown"
  },
  {
    "Atom Number": 115,
    "Name": "Moscovium",
    "Symbol": "Mc",
    "Type": "Unknown",
    "Serie": "Unknown"
  },
  {
    "Atom Number": 116,
    "Name": "Livermorium",
    "Symbol": "Lv",
    "Type": "Unknown",
    "Serie": "Unknown"
  },
  {
    "Atom Number": 117,
    "Name": "Tennessine",
    "Symbol": "Ts",
    "Type": "Unknown",
    "Serie": "Unknown"
  },
  {
    "Atom Number": 118,
    "Name": "Oganesson",
    "Symbol": "Og",
    "Type": "Unknown",
    "Serie": "Unknown"
  }
]
 
Çözüm
Şu an denedim gayet sağlıklı çalışıyor, istersen yazdığın kodu paylaş ona göre düzenleme yapalım.
Adsız.png


C++:
#include <iostream>
#include <fstream>
#include <json/json.h>

using namespace std;

int main() {
    ifstream ifs("alice.json");
    Json::Reader readers;
    Json::Value obj;
    readers.parse(ifs, obj);
    const Json::Value& characters = obj;
    for (int i = 0; i < characters.size(); i++){
        cout << "Number: " << characters[i]["Atom Number"].asUInt() << endl;
        cout << "Name: " << characters[i]["Name"].asString()<< endl;
        cout << "Symbol: " << characters[i]["Symbol"].asString()<< endl;
        cout << "Type: " << characters[i]["Type"].asString()<< endl;
        cout << "Serie: " << characters[i]["Serie"].asString()<< endl;
        cout << endl;
    }
}
Dokümanlarında örnek kodlar var, reader sınıfından bir nesne oluşturup okuma işlemini bununla yapman gerekiyor gibi


JSON:
{
    "book":"Alice in Wonderland",
    "year":1865,
    "characters":
    [
        {"name":"Jabberwock", "chapter":1},
        {"name":"Cheshire Cat", "chapter":6},
        {"name":"Mad Hatter", "chapter":7}
    ]
}

C++:
#include <iostream>
#include <fstream>
#include <jsoncpp/json/json.h> // or jsoncpp/json.h , or json/json.h etc.

using namespace std;

int main() {
    ifstream ifs("alice.json");
    Json::Reader reader;
    Json::Value obj;
    reader.parse(ifs, obj); // reader can also read strings
    cout << "Book: " << obj["book"].asString() << endl;
    cout << "Year: " << obj["year"].asUInt() << endl;
    const Json::Value& characters = obj["characters"]; // array of characters
    for (int i = 0; i < characters.size(); i++){
        cout << "    name: " << characters[i]["name"].asString();
        cout << " chapter: " << characters[i]["chapter"].asUInt();
        cout << endl;
    }
}
 
Dokümanlarında örnek kodlar var, reader sınıfından bir nesne oluşturup okuma işlemini bununla yapman gerekiyor gibi


JSON:
{
    "book":"Alice in Wonderland",
    "year":1865,
    "characters":
    [
        {"name":"Jabberwock", "chapter":1},
        {"name":"Cheshire Cat", "chapter":6},
        {"name":"Mad Hatter", "chapter":7}
    ]
}

C++:
#include <iostream>
#include <fstream>
#include <jsoncpp/json/json.h> // or jsoncpp/json.h , or json/json.h etc.

using namespace std;

int main() {
    ifstream ifs("alice.json");
    Json::Reader reader;
    Json::Value obj;
    reader.parse(ifs, obj); // reader can also read strings
    cout << "Book: " << obj["book"].asString() << endl;
    cout << "Year: " << obj["year"].asUInt() << endl;
    const Json::Value& characters = obj["characters"]; // array of characters
    for (int i = 0; i < characters.size(); i++){
        cout << "    name: " << characters[i]["name"].asString();
        cout << " chapter: " << characters[i]["chapter"].asUInt();
        cout << endl;
    }
}
Maalesef hata alıyorum.
C++:
terminate called after throwing an instance of 'Json::LogicError' what(): in Json::Value::resolveReference(key, end): requires objectValue İptal edildi (çekirdek döküldü)

@Linuxginar. Hocam nasıl implement ederiz?
 
Şu an denedim gayet sağlıklı çalışıyor, istersen yazdığın kodu paylaş ona göre düzenleme yapalım.
Adsız.png


C++:
#include <iostream>
#include <fstream>
#include <json/json.h>

using namespace std;

int main() {
    ifstream ifs("alice.json");
    Json::Reader readers;
    Json::Value obj;
    readers.parse(ifs, obj);
    const Json::Value& characters = obj;
    for (int i = 0; i < characters.size(); i++){
        cout << "Number: " << characters[i]["Atom Number"].asUInt() << endl;
        cout << "Name: " << characters[i]["Name"].asString()<< endl;
        cout << "Symbol: " << characters[i]["Symbol"].asString()<< endl;
        cout << "Type: " << characters[i]["Type"].asString()<< endl;
        cout << "Serie: " << characters[i]["Serie"].asString()<< endl;
        cout << endl;
    }
}
 
Son düzenleyen: Moderatör:
Çözüm
Şu an denedim gayet sağlıklı çalışıyor, istersen yazdığın kodu paylaş ona göre düzenleme yapalım.
Eki Görüntüle 1240650

C++:
#include <iostream>
#include <fstream>
#include <json/json.h>

using namespace std;

int main() {
    ifstream ifs("alice.json");
    Json::Reader readers;
    Json::Value obj;
    readers.parse(ifs, obj);
    const Json::Value& characters = obj;
    for (int i = 0; i < characters.size(); i++){
        cout << "Number: " << characters[i]["Atom Number"].asUInt() << endl;
        cout << "Name: " << characters[i]["Name"].asString()<< endl;
        cout << "Symbol: " << characters[i]["Symbol"].asString()<< endl;
        cout << "Type: " << characters[i]["Type"].asString()<< endl;
        cout << "Serie: " << characters[i]["Serie"].asString()<< endl;
        cout << endl;
    }
}
Allah Allah pek garip. Sizin attığınız kod çalıştı. Teşekkürler. Bu arada bu for loopu. Ben atom numarasını (ya da atom ismini veya sembolünü) yazarak o atom numarasıyla eşleşen kaydın bütün değerlerini göstermek istiyorum.
 
Onu yapmak kolay, halihazırda döngü bütün diziyi dolaşıyor bir if yordamıyla kullanıcının girdiği değerle eşleşeni bulup ekrana bas ve break ile döngüden çık gibi... bu arada string girdi alırken std::getline (std::cin, std::string variable); şeklinde kullan.
 

Geri
Yukarı