2010-11-25 6 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 열에 구문 오류가 있으며 값, 개체 또는 배열이 있어야합니다. 누구든지이 문제를 해결하는 방법을 알고 있습니까?

편집 : 페이스트 빈에서, 현재 코드에 변경이

+2

, 당신의 코드를 포맷 그것을하고 Ctrl + K를 선택합니다. 미리보기를 사용하십시오. – EboMike

+0

Downvote는 다음과 같습니다. 함수 인수를 임의로 추측하는 대신 문서 _을 (를) 간단히 읽으십시오. 그러면 괜찮을 것입니다. –

+1

@LightnessRacesinOrbit 실제로이 인수에 대한 문서는 "읽을 문서가 들어있는 UTF-8 인코딩 된 문자열"을 읽습니다. _ 사용되는 단어 'document'에서 추측 할 수있을만큼 적어도 모호한 것은 _ std :: 요청한 문자열은 실제로 파일 경로 여야합니다. 사실, 나는 OP와 똑같은 실수를 저질렀다. – Tom

답변

26

Json::Reader::parse 설명서를 참조하십시오. 해당 오버로드의 경우 문자열은 파일 이름이 아닌 실제 문서 여야합니다.

istream overloadifstream으로 대신 사용할 수 있습니다.

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

@Yelnats, 이제 입력 파일로 위 테스트를 마쳤습니다. –

+0

이것은 미친 짓이며, 저에게 효과적이지 않습니다. testis.json 파일을 프로젝트 디렉토리에 넣으시겠습니까? C : \ Programs \ TestJSON \ TestJSON이 될 것입니다. – Yelnats

-12

JSON은 줄 바꿈을 포함 할 수 없습니다 : 나는 그것을 작업 얻었다. 대신 다음을 시도하십시오 :

{"encoding": "lab"} 

마지막 줄 바꿈없이 파일을 저장해야 할 수도 있습니다.

EDIT : 파서가 개행을 허용 할 수도 있지만 일부는 그렇지 않을 수 있습니다. 다른 답변이 더 일반적으로

+2

예, 가능하며 이는 문제가 아닙니다. 그렇지 않으면 파서가 파기되어야합니다. [RFC 4627] (http://tools.ietf.org/html/rfc4627) : "6 개의 구조 문자 앞뒤에 공백 문자를 사용할 수 없습니다." 공백의 정의에는 명시 적으로 CR과 LF가 모두 포함됩니다. –

+1

JSON은 절대적으로 개행을 포함 할 수 있습니다. 무슨 말도 안돼. –

+0

@Michael은 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

관련 문제