2016-07-18 2 views
0

작은 응용 프로그램을 위해 Unreal Engine에서 로컬 디스크를 탐색하는 프로그램을 작성하려고합니다. Gradle을 사용하여 REST 서버를한데 모아 놓았습니다. 짧게 말하면 JSON에 기계 디렉토리가 있습니다. 특정 문자열 이름 (특히 FText, 여기서는 너무 중요하지 않음) 배열로 반환되도록 특정 디렉터리 이름을 끌어오고 싶습니다.C++에서 JSON 값에 액세스

nLohmann이 github (https://github.com/nlohmann/json)로 만든 라이브러리를 발견했습니다.이 라이브러리는 JSON을 C++로 처리하는 가장 좋은 방법 인 것 같습니다. 그러나 내 인생에서 디렉토리 이름을 빼는 방법을 알 수는 없습니다. 나는 iterator와 똑바로 .value() 호출을 시도했다.

코드와 JSON 예제는 아래에 나와 있습니다. 어떤 통찰력이라도 대단히 감사하겠습니다.

char buffer[1024]; 

FILE *lsofFile_p = _popen("py C:\\Users\\jinx5\\CWorkspace\\sysCalls\\PullRoots.py", "r"); 
fgets(buffer, sizeof(buffer), lsofFile_p); 
_pclose(lsofFile_p); 

std::string rootsJson(buffer); 
string s = rootsJson.substr(1); 
s = ReplaceAll(s, "'", ""); 

//here my string s will contain: [{"description":"Local Disk","name":"C:\\"},{"description":"Local Disk","name":"D:\\"},{"description":"CD Drive","name":"E:\\"}] 


//These are two syntax examples I found un nlohmann's docs, neither seems to work 
auto j = json::parse(s); 
string descr = j.value("description", "err"); 
+0

또 다른 C++ JSON 옵션 (즉, * 내가 아주 좋아 *) : // 여기

은 작업 예입니다 github.com/kazuho/picojson/blob/master/README.mkdn). –

+0

아마도 당신에게 너무 무거울 것입니다.하지만 Qt의 C++ Json 구현도 사용합니다. 그것은 정말 잘 작동합니다. –

+0

"어느 쪽도 효과가없는 것 같습니다"는 유용한 문제 설명이 아닙니다. "안녕, 밥의 자동차 수리? 내 차가 작동하지 않는 것 같아?" –

답변

2

귀하의 문자가 \ 인 것으로 생각됩니다. C:\\의 경우 5 \ : C:\\\\\이 필요합니다. 이다 PicoJSON] (https://github.com/kazuho/picojson) (HTTPS

#include "json.hpp" 
#include <string> 

using namespace std; 
using json = nlohmann::json; 

int main(){ 

    json j = json::parse("[{\"description\":\"Local Disk\",\"name\":\"C:\\\\\"},{\"description\":\"Local Disk\",\"name\":\"D:\\\\\"},{\"description\":\"CD Drive\",\"name\":\"E:\\\\\"}]"); 

    cout << j.is_array() << endl; 

    for (auto& element : j) { 
     std::cout << "description : " << element["description"] << " | " << " name : " << element["name"] << '\n'; 
    } 
    return 0; 
}