2010-11-25 11 views
11

तो मैं हाल ही में JSONCPP स्थापित किया है और किसी कारण से यह मेरे त्रुटियों देता है जब मैं इस कोड का प्रयास करें:JSONCPP पढ़ने नहीं फ़ाइलों को सही ढंग

#include <json.h> 
#include <iostream> 
#include <fstream> 

int main(){ 
    bool alive = true; 
    while (alive){ 
    Json::Value root; // will contains the root value after parsing. 
    Json::Reader reader; 
    std::string test = "testis.json"; 
    bool parsingSuccessful = reader.parse(test, root, false); 
    if (!parsingSuccessful) 
    { 
     // report to the user the failure and their locations in the document. 
     std::cout << reader.getFormatedErrorMessages() 
       << "\n"; 
    } 

    std::string encoding = root.get("encoding", "UTF-8").asString(); 
    std::cout << encoding << "\n"; 
    alive = false; 


    } 
    return 0; 
} 

और यहाँ फ़ाइल है:

{ 
"encoding" : "lab" 
} 

इसमें कहा गया है कि लाइन 1, कॉलम 1 पर एक वाक्यविन्यास त्रुटि है, और यह एक मान, वस्तु या सरणी होना चाहिए। क्या किसी को पता है कि इसे किस प्रकार ठीक किया जा सकता है?

संपादित करें:, वर्तमान कोड में बदला गया pastebin से

+2

अपने कोड फ़ॉर्मेट करने के लिए है, यह और Ctrl + K प्रेस का चयन करें। पूर्वावलोकन का प्रयोग करें। – EboMike

+0

डाउनवोट इसलिए है क्योंकि: फ़ंक्शन तर्कों पर यादृच्छिक रूप से अनुमान लगाने के बजाय दस्तावेज़_ को बस _read करें, और आप ठीक होंगे। –

+1

@LightnessRacesinOrbit वास्तव में, इस तर्क के लिए प्रलेखन _ "यूटीएफ -8 एन्कोडेड स्ट्रिंग को पढ़ने के लिए दस्तावेज़ को पढ़ता है।" _ जो कम से कम थोड़ा अस्पष्ट है क्योंकि कोई 'दस्तावेज़' शब्द से अनुमान लगा सकता है कि std :: अनुरोध किया गया स्ट्रिंग वास्तव में एक फ़ाइल पथ होना चाहिए। असल में, मैंने ओपी के समान ही गलती की है। – Tom

उत्तर

26

Json::Reader::parse दस्तावेज़ देखें। उस अधिभार के लिए, स्ट्रिंग को वास्तविक दस्तावेज़ होना चाहिए, फ़ाइल नाम नहीं।

आप ifstream के साथ istream overload का उपयोग कर सकते हैं।

std::ifstream test("testis.json", std::ifstream::binary); 

संपादित करें:

#include "json/json.h" 
#include <iostream> 
#include <fstream> 

int main(){ 
    bool alive = true; 
    while (alive){ 
    Json::Value root; // will contains the root value after parsing. 
    Json::Reader reader; 
    std::ifstream test("testis.json", std::ifstream::binary); 
    bool parsingSuccessful = reader.parse(test, root, false); 
    if (!parsingSuccessful) 
    { 
     // report to the user the failure and their locations in the document. 
     std::cout << reader.getFormatedErrorMessages() 
       << "\n"; 
    } 

    std::string encoding = root.get("encoding", "UTF-8").asString(); 
    std::cout << encoding << "\n"; 
    alive = false; 
    } 
    return 0; 
} 
+0

यह अभी भी मुझे एक ही त्रुटि देता है। आप इसे वास्तविक दस्तावेज़ कैसे बनाते हैं? – Yelnats

+0

@ येलनेट्स, मैंने अब उपरोक्त परीक्षण फ़ाइल के साथ परीक्षण किया है। –

+0

यह पागल है, यह मेरे लिए काम नहीं कर रहा है। मैं testis.json फ़ाइल को परियोजना की निर्देशिका में सही जगह पर रखता हूं? तो यह सी: \ प्रोग्राम \ TestJSON \ TestJSON सही होगा? – Yelnats

-12

json नई-पंक्तियों को शामिल नहीं कर सकते हैं: मैं इसके साथ काम मिल गया। इसके बजाय इसे आजमाएं:

{"encoding": "lab"} 

आपको यह सुनिश्चित करने की आवश्यकता हो सकती है कि फ़ाइल अंतिम रूपरेखा के बिना सहेजी गई हो।

संपादित करें: हो सकता है कि आपका पार्सर न्यूलाइन सहन करे, लेकिन कुछ नहीं। कुछ करता है, तो अन्य उत्तर

+2

हां यह कर सकता है, और यह मुद्दा नहीं है। कोई भी पार्सर जो अन्यथा कहता है उसे छीन लिया जाना चाहिए। [आरएफसी 4627] (http://tools.ietf.org/html/rfc4627): "छः संरचनात्मक पात्रों में से किसी एक के पहले या बाद में महत्वहीन व्हाइटस्पेस की अनुमति है।" और व्हाइटस्पेस की परिभाषा में स्पष्ट रूप से सीआर और एलएफ दोनों शामिल हैं। –

+1

JSON पूरी तरह से न्यूलाइन रख सकता है। क्या बकवास। –

+0

@ माइकल उस मामले में यह JSON पार्सर नहीं है http://www.ietf.org/rfc/rfc4627.txt – sehe

0
#include "json/json.h" 
#include <iostream> 
#include <fstream> 

int main(){ 
    Json::Value root; // will contain the root value after parsing. 
    std::ifstream stream("testis.json", std::ifstream::binary); 
    stream >> root; 
    std::string encoding = root.get("encoding", "UTF-8").asString(); 
    std::cout << encoding << "\n"; 
    return 0; 
} 

काम नहीं करते या अधिक आम तौर पर कोशिश करने के लिए:

#include "json/json.h" 
#include <iostream> 
#include <fstream> 

int main(){ 
    Json::Value root; // will contain the root value after parsing. 
    Json::CharReaderBuilder builder; 
    std::ifstream test("testis.json", std::ifstream::binary); 
    std::string errs; 
    bool ok = Json::parseFromStream(builder, test, &root, &errs); 
    if (!ok) 
    { 
     // report to the user the failure and their locations in the document. 
     std::cout << errs << "\n"; 
    } 

    std::string encoding = root.get("encoding", "UTF-8").asString(); 
    std::cout << encoding << "\n"; 
    return 0; 
} 

http://open-source-parsers.github.io/jsoncpp-docs/doxygen/namespace_json.html

संबंधित मुद्दे