Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read some data in json file #1843

Closed
zj1838277795 opened this issue Nov 19, 2019 · 2 comments
Closed

Cannot read some data in json file #1843

zj1838277795 opened this issue Nov 19, 2019 · 2 comments

Comments

@zj1838277795
Copy link

I am a newbie. I am doing an Android web application, the code for parsing the json file is as follows
{
"name":"HelloWorld",
"description":"Simple web example application to demonstrate",
"package":"com.example.helloworld",
"version":"0.1.0",
"start_url":"index.html",
"icons":[
{
"src":"res/img/appicon.png"
}
],
"developer":{
"name":"Example Developer",
"url":"http://www.example.com/",
"email":"[email protected]"
}
}
and my cpp code is as follows

std::string showJson(std::string cni, std::string cnj){
using json = nlohmann::json;
std::ifstream fin("/data/data/iecas.jsonshow.commanager/lib/manifest.json");
if(!fin) return "fin==null";
json j;
fin >> j;
fin.close();
if(cni == "name"&&cnj==" ") return j["name"];
if(cni == "description"&&cnj==" ") return j["description"];
if(cni == "package"&&cnj==" ") return j["package"];
if(cni == "version"&&cnj==" ") return j["version"];
if(cni == "start_url"&&cnj==" ") return j["start_url"];
if(cni == "developer"&&cnj=="name") return j["developer"]["name"];
if(cni == "developer"&&cnj=="url") return j["developer"]["url"];
if(cni == "developer"&&cnj=="email") return j["developer"]["email"];
if(cni == "developer"&&cnj==" ") return j["developer"];
if(cni == "icons"&&cnj==" ") return j["icons"];
}

I want to know how to read the string of "icons". When i use j["icons"] or j["developer"], my web app has crashed. j["developer"]["name"] and j["name"] are no problem.

@gregmarr
Copy link
Contributor

The icons and developer entries aren't strings. The first is an array, and the second is an object. You can't convert them to a string like that. If you really do want the full JSON content of that entry as a string, you can use j["icons"].dump(), which will give you a string like this:

[{ "src": "res/img/appicon.png" }]

@zj1838277795
Copy link
Author

iconsdeveloper项不是字符串。第一个是数组,第二个是对象。您不能将它们转换为这样的字符串。如果您确实希望该条目的完整JSON内容作为字符串,则可以使用j["icons"].dump(),它将为您提供如下字符串:

[{ "src": "res/img/appicon.png" }]

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants