如何修改QJsonObject
How to modify QJsonObject
我可以在简单的 json 文件中以这种方式为对象设置值。
Qt代码:
if(file.open(QIODevice::ReadOnly)){
QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
QJsonObject obj = doc.object();
obj["gender"] = "female";
doc.setObject(obj);
manager.put(request, doc.toJson());
}
简单json文件:
{
"resourceType": "Patient",
"gender": "male",
"birthDate": "2018-08-21"
}
因此这段代码将性别的值更改为女性。
但是当我使用更大的 json 时 "objects under objects"
{
"resource": {
"resourceType": "Patient",
"id": "530f8eb0-1f0b-41b0-b94",
"text": {
"status": "generated",
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">Teppo Testman\n <a name=\"mm\"></a>\n </div>"
},
"name": [{
"family": "Testman",
"given": [
"Teppo"
]
}],
"gender": "male",
"birthDate": "1993-02-12"
}
}
更新:
{
"resourceType": "Bundle",
"id": "17772d63-3b14-494f-8182-06b67c970550",
"meta": {
"versionId": "1",
"lastUpdated": "2018-08-15T12:08:48.036+00:00"
},
"type": "collection",
"entry": [{
"resource": {
"resourceType": "Observation",
"text": {
"status": "generated",
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">Height\n <a name=\"mm\"></a>\n </div>"
},
"status": "final",
"code": {
"coding": [{
"system": "http://loinc.org",
"code": "8302-2",
"display": "Body height"
}]
},
"subject": {
"reference": "Patient/530f8eb0-1f0b-41b0-b94b-c25a14656645"
},
"valueQuantity": {
"value": 166,
"unit": "cm"
}
}
},
{
"resource": {
"resourceType": "Observation",
"text": {
"status": "generated",
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">BMI\n <a name=\"mm\"></a>\n </div>"
},
"status": "final",
"code": {
"coding": [{
"system": "http://loinc.org",
"code": "39156-5",
"display": "Body mass index (BMI) [Ratio]"
}]
},
"subject": {
"reference": "Patient/530f8eb0-1f0b-41b0-b94b-c25a14656645"
},
"valueString": "23"
}
},
{
"resource": {
"resourceType": "Observation",
"text": {
"status": "generated",
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">FEV1 [l]\n <a name=\"mm\"></a>\n </div>"
},
"status": "final",
"code": {
"coding": [{
"system": "http://loinc.org",
"code": "18759-1",
"display": "Spirometry study"
}]
},
"subject": {
"reference": "Patient/530f8eb0-1f0b-41b0-b94b-c25a14656645"
},
"valueQuantity": {
"value": 2,
"unit": "l"
}
}
},
{
"resource": {
"resourceType": "Observation",
"text": {
"status": "generated",
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">FEV6 [l]\n <a name=\"mm\"></a>\n </div>"
},
"status": "final",
"code": {
"coding": [{
"system": "http://loinc.org",
"code": "18759-1",
"display": "Spirometry study"
}]
},
"subject": {
"reference": "Patient/530f8eb0-1f0b-41b0-b94b-c25a14656645"
},
"valueQuantity": {
"value": 2.9,
"unit": "l"
}
}
},
{
"resource": {
"resourceType": "Observation",
"text": {
"status": "generated",
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">PEF [l/s]\n <a name=\"mm\"></a>\n </div>"
},
"status": "final",
"code": {
"coding": [{
"system": "http://loinc.org",
"code": "18759-1",
"display": "Spirometry study"
}]
},
"subject": {
"reference": "Patient/530f8eb0-1f0b-41b0-b94b-c25a14656645"
},
"valueQuantity": {
"value": 6.5,
"unit": "l/s"
}
}
},
{
"resource": {
"resourceType": "Patient",
"id": "530f8eb0-1f0b-41b0-b94b-c25a14656645",
"text": {
"status": "generated",
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">Teppo Testman\n <a name=\"mm\"></a>\n </div>"
},
"name": [{
"family": "Testman",
"given": [
"Teppo"
]
}],
"gender": "male",
"birthDate": "1993-02-12"
}
}
]
}
我无法修改该值。有一些简单的方法可以做到这一点吗?我一直在努力寻找解决方案但没有成功!
json不是一个线性结构,所以你要通过keys arbor找,这里有一个gender
就是[=的children 13=],因此首先您将访问 resource
,然后是 gender
,如下所示:
if(file.open(QIODevice::ReadOnly)){
QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
QJsonObject root = doc.object();
QJsonObject res = root["resource"].toObject();
res["gender"] = "female";
root["resource"] = res;
doc.setObject(root);
//another code
manager.put(request, doc.toJson());
}
更新:
思路是一样的,不能直接访问,但要知道json的结构。虽然我有一个观察,例如条目的项目 children 是一个数组,但正常情况是那些 children 具有相同的结构,但在你的情况下,最后一个是不同的,这是不寻常的。
if(file.open(QIODevice::ReadOnly)){
QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
QJsonObject root = doc.object();
QJsonArray entries = root["entry"].toArray();
for(int i=0; i < entries.count(); i++){
QJsonObject obj = entries[i].toObject();
QJsonObject objs = obj["resource"].toObject();
if(objs.contains("gender")){
objs["gender"]= "female";
obj["resource"] = objs;
entries[i] = obj;
}
}
root["entry"] = entries;
doc.setObject(root);
// another process
}
我可以在简单的 json 文件中以这种方式为对象设置值。
Qt代码:
if(file.open(QIODevice::ReadOnly)){
QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
QJsonObject obj = doc.object();
obj["gender"] = "female";
doc.setObject(obj);
manager.put(request, doc.toJson());
}
简单json文件:
{
"resourceType": "Patient",
"gender": "male",
"birthDate": "2018-08-21"
}
因此这段代码将性别的值更改为女性。
但是当我使用更大的 json 时 "objects under objects"
{
"resource": {
"resourceType": "Patient",
"id": "530f8eb0-1f0b-41b0-b94",
"text": {
"status": "generated",
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">Teppo Testman\n <a name=\"mm\"></a>\n </div>"
},
"name": [{
"family": "Testman",
"given": [
"Teppo"
]
}],
"gender": "male",
"birthDate": "1993-02-12"
}
}
更新:
{
"resourceType": "Bundle",
"id": "17772d63-3b14-494f-8182-06b67c970550",
"meta": {
"versionId": "1",
"lastUpdated": "2018-08-15T12:08:48.036+00:00"
},
"type": "collection",
"entry": [{
"resource": {
"resourceType": "Observation",
"text": {
"status": "generated",
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">Height\n <a name=\"mm\"></a>\n </div>"
},
"status": "final",
"code": {
"coding": [{
"system": "http://loinc.org",
"code": "8302-2",
"display": "Body height"
}]
},
"subject": {
"reference": "Patient/530f8eb0-1f0b-41b0-b94b-c25a14656645"
},
"valueQuantity": {
"value": 166,
"unit": "cm"
}
}
},
{
"resource": {
"resourceType": "Observation",
"text": {
"status": "generated",
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">BMI\n <a name=\"mm\"></a>\n </div>"
},
"status": "final",
"code": {
"coding": [{
"system": "http://loinc.org",
"code": "39156-5",
"display": "Body mass index (BMI) [Ratio]"
}]
},
"subject": {
"reference": "Patient/530f8eb0-1f0b-41b0-b94b-c25a14656645"
},
"valueString": "23"
}
},
{
"resource": {
"resourceType": "Observation",
"text": {
"status": "generated",
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">FEV1 [l]\n <a name=\"mm\"></a>\n </div>"
},
"status": "final",
"code": {
"coding": [{
"system": "http://loinc.org",
"code": "18759-1",
"display": "Spirometry study"
}]
},
"subject": {
"reference": "Patient/530f8eb0-1f0b-41b0-b94b-c25a14656645"
},
"valueQuantity": {
"value": 2,
"unit": "l"
}
}
},
{
"resource": {
"resourceType": "Observation",
"text": {
"status": "generated",
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">FEV6 [l]\n <a name=\"mm\"></a>\n </div>"
},
"status": "final",
"code": {
"coding": [{
"system": "http://loinc.org",
"code": "18759-1",
"display": "Spirometry study"
}]
},
"subject": {
"reference": "Patient/530f8eb0-1f0b-41b0-b94b-c25a14656645"
},
"valueQuantity": {
"value": 2.9,
"unit": "l"
}
}
},
{
"resource": {
"resourceType": "Observation",
"text": {
"status": "generated",
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">PEF [l/s]\n <a name=\"mm\"></a>\n </div>"
},
"status": "final",
"code": {
"coding": [{
"system": "http://loinc.org",
"code": "18759-1",
"display": "Spirometry study"
}]
},
"subject": {
"reference": "Patient/530f8eb0-1f0b-41b0-b94b-c25a14656645"
},
"valueQuantity": {
"value": 6.5,
"unit": "l/s"
}
}
},
{
"resource": {
"resourceType": "Patient",
"id": "530f8eb0-1f0b-41b0-b94b-c25a14656645",
"text": {
"status": "generated",
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">Teppo Testman\n <a name=\"mm\"></a>\n </div>"
},
"name": [{
"family": "Testman",
"given": [
"Teppo"
]
}],
"gender": "male",
"birthDate": "1993-02-12"
}
}
]
}
我无法修改该值。有一些简单的方法可以做到这一点吗?我一直在努力寻找解决方案但没有成功!
json不是一个线性结构,所以你要通过keys arbor找,这里有一个gender
就是[=的children 13=],因此首先您将访问 resource
,然后是 gender
,如下所示:
if(file.open(QIODevice::ReadOnly)){
QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
QJsonObject root = doc.object();
QJsonObject res = root["resource"].toObject();
res["gender"] = "female";
root["resource"] = res;
doc.setObject(root);
//another code
manager.put(request, doc.toJson());
}
更新:
思路是一样的,不能直接访问,但要知道json的结构。虽然我有一个观察,例如条目的项目 children 是一个数组,但正常情况是那些 children 具有相同的结构,但在你的情况下,最后一个是不同的,这是不寻常的。
if(file.open(QIODevice::ReadOnly)){
QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
QJsonObject root = doc.object();
QJsonArray entries = root["entry"].toArray();
for(int i=0; i < entries.count(); i++){
QJsonObject obj = entries[i].toObject();
QJsonObject objs = obj["resource"].toObject();
if(objs.contains("gender")){
objs["gender"]= "female";
obj["resource"] = objs;
entries[i] = obj;
}
}
root["entry"] = entries;
doc.setObject(root);
// another process
}