2016-10-11 5 views
1

JSON에서 생성 된 variantMaps가 3 개 있습니다. 예를 들어
을 두 번째로 두 번째 이상의 세 가지를 먼저 속성으로 대체하십시오.json의 필드를 QVariantMaps를 사용하는 Qt 5.7의보다 쉬운 방법론을 사용하여 대체합니다.

QVariantMap wholeMapToChange;   //1.   
    QVariantMap propertiesMapToChange; //2. 
    QVariantMap cmdMap;     //3. 

1이 JSON 데이터를 포함하지만 맵 :

{ 
    properties { 
     "A": true, 
     "B": true, 
     "fieldName": "ewfqfqewf", 
     "C": false, 
     "fieldPassword": "451541611", 
     "isBtnSignOnClicked": true 
    }, 
    type: "xyz" 
} 

는 2이 JSON 데이터를 포함하지만 맵 :

{ 
"A": true, 
"B": true, 
"fieldName": "ewfqfqewf", 
"C": false, 
"fieldPassword": "451541611", 
"isBtnSignOnClicked": true 
} 

이 JSON 데이터 만 맵에 포함되어 차원 :

{ 
"fieldName": "nick", 
"fieldPassword": "0000", 
"isBtnSignOnClicked": true 
} 

내가 볼 수있는 것 2와 3을 substituing에 대한 lity 사이클을

for (QVariantMap::const_iterator it = propertiesMapToChange.begin(); it != propertiesMapToChange.end(); ++it){ 

    for (QVariantMap::const_iterator itt = cmdMap.begin(); itt != cmdMap.end(); ++itt){ 

     ///here would be the comparig... 

    } 
    } 

를 만드는 것입니다하지만 내가 올바른 여부, 조언이나 의견을 부탁하거나 할 수있는 더 좋은 방법이있을 것입니다 ...이 좋은 해결책이라고 생각하지 말아 그.

들으

+0

참고 : 당신의 1 JSON 예제가 잘못 –

답변

1

지도가 너무 크게하지 않으면 비용이 N의 *의 M이기 때문에 그것은 오른쪽 솔루션입니다. 그러나 조기에 비관적이다. N + M 비용으로 루프를 구현해야합니다. 결국지도가 정렬되므로 각지도를 한 번만 반복하면됩니다.

완전한 예 :

// https://github.com/KubaO/stackoverflown/tree/master/questions/json-map-iter-39979440 
#include <QtCore> 

QVariantMap replaceMap(QVariantMap dst, const QVariantMap & src) { 
    auto dit = dst.begin(); 
    auto sit = src.begin(); 
    while (dit != dst.end() && sit != src.end()) { 
     if (sit.key() < dit.key()) { 
      ++ sit; 
      continue; 
     } 
     if (dit.key() < sit.key()) { 
      ++ dit; 
      continue; 
     } 
     Q_ASSERT(sit.key() == dit.key()); 
     dit.value() = sit.value(); 
     ++ sit; 
     ++ dit; 
    } 
    return dst; 
} 

int main() { 
    auto json1 = QJsonDocument::fromJson({R"ZZ({ 
    "properties":{ 
     "A":true, 
     "B":true, 
     "fieldName":"ewfqfqewf", 
     "C":false, 
     "fieldPassword":"451541611", 
     "isBtnSignOnClicked":true 
    }, 
    "type":"xyz" 
    })ZZ"}).toVariant().value<QVariantMap>(); 

    auto json2 = QJsonDocument::fromJson({R"ZZ({ 
    "A":true, 
    "B":true, 
    "fieldName":"ewfqfqewf", 
    "C":false, 
    "fieldPassword":"451541611", 
    "isBtnSignOnClicked":true 
    })ZZ"}).toVariant().value<QVariantMap>(); 

    auto json3 = QJsonDocument::fromJson(
    {R"ZZ({ 
    "fieldName":"nick", 
    "fieldPassword":"0000", 
    "isBtnSignOnClicked":true 
    })ZZ"}).toVariant().value<QVariantMap>(); 

    json2 = replaceMap(json2, json3); 
    auto props = replaceMap(json1["properties"].value<QVariantMap>(), json2); 
    json1["properties"] = props; 

    qDebug() << QJsonDocument::fromVariant(json1).toJson().constData(); 
} 

출력 :

{ 
    "properties": { 
     "A": true, 
     "B": true, 
     "C": false, 
     "fieldName": "nick", 
     "fieldPassword": "0000", 
     "isBtnSignOnClicked": true 
    }, 
    "type": "xyz" 
} 
+0

매우 도움이, 감사 –

관련 문제