借助c ++

时间:2018-05-07 04:47:22

标签: c++ json rapidjson

我目前正在开发一个项目,通过从json模式获取的路径从Json中检索值,因为我只想从Json获取json模式中存在的那些键的值。

我需要检索值的路径:

/array/0
/boolean
/null
/object/a
/object/c
/object/e
/number
/deep_nested_array/0/object_array1/0/key4
/deep_nested_array/0/object_array1/1/key5
/deep_nested_array/0/object_array1/2/key6
/object_array/0/key1
/object_array/1/key2
/object_array/2/key3
/string

从我想要从中检索值的Json:

{
 "array": [
   1,
   2,
   3
],
"boolean": true,
"null": null,
"number": 123,
"object": {
  "a": "b",
  "c": "d",
  "e": "f"
 },
 "string": "Hello World", 
 "object_array": [
    {"key1": "value1" },
    {"key2": "value2" },
    {"key3": "value3" }
    ],
  "deep_nested_array": [
       {"object_array1": [
          {"key4": "value4" },
          {"key5": "value5" },
          {"key6": "value6" }
       ]}
        ,
       {"object_array2": [
          {"key7": "value7" },
          {"key8": "value8" },
          {"key9": "value9" }
       ]}
     ]
 }

我只想回来

[
   1,
   2,
   3
]
true
null
b
d
f
123
Hello World
value1
value2
value3
value4
value5
value6

订单将取决于通过的路径,无所谓。

我的代码

  static string findValue(const Value &item, string path) {
    StringBuffer s1;
    Writer<StringBuffer> w1(s1);
    item.Accept(w1);
    cout << "recursing:" << s1.GetString() << endl;
    cout << "Original Path:" << path << endl;
    string delimiter = "/";
    size_t pos = 1;
    string token, paths;

    token = path.substr(0, path.find(delimiter));
    cout << "token:" << token << endl;
    const Value &element = item[token.c_str()];
    StringBuffer s;
    Writer<StringBuffer> w(s);
    element.Accept(w);
    cout << "items matching the token:" << s.GetString() << endl;
    while ((pos = path.find(delimiter)) <= path.find(delimiter)) {
        paths = path.erase(0, pos + delimiter.length());
        cout << "paths:" << paths << endl;


        if (paths.length() > 1) {
            if (element.IsObject()) {


                findValue(element, paths);
            } else if (element.IsArray()) {

                for (SizeType i = 0; i < element.Size(); i++) {
                    if (element[i].IsArray() || element[i].IsObject()) {
                        findValue(element[i], paths);
                    } else {
                        cout << "Array returning:" << s.GetString() << endl;
                        return s.GetString();
                    }
                }
            }

        } else {
            cout << "item returningg:" << s.GetString() << endl;
            return s.GetString();

        }
    }

}

我的输出

recursing:{"array": 
[1,2,3],"boolean":true,"null":null,"number":123,"object": 
{"a":"b","c":"d","e":"f"},"string":"Hello World","object_array": 
[{"key1":"value1"},{"key2":"value2"}, 
{"key3":"value3"}],"deep_nested_array":[{"object_array1": 
[{"key4":"value1"},{"key5":"value2"},{"key6":"value3"}]}, 
{"object_array2":[{"key7":"value4"},{"key8":"value5"}, 
{"key9":"value6"}]}]}

Original Path:array/
token:array
items matching the token:[1,2,3]
paths:
item returningg:[1,2,3]

recursing:{"array": 
[1,2,3],"boolean":true,"null":null,"number":123,"object": 
{"a":"b","c":"d","e":"f"},"string":"Hello World","object_array": 
[{"key1":"value1"},{"key2":"value2"}, 
{"key3":"value3"}],"deep_nested_array":[{"object_array1": 
[{"key4":"value1"},{"key5":"value2"},{"key6":"value3"}]}, 
{"object_array2":[{"key7":"value4"},{"key8":"value5"}, 
{"key9":"value6"}]}]}

Original Path:boolean/
token:boolean
items matching the token:true
paths:
item returningg:true

recursing:{"array": 
[1,2,3],"boolean":true,"null":null,"number":123,"object": 
{"a":"b","c":"d","e":"f"},"string":"Hello World","object_array": 
[{"key1":"value1"},{"key2":"value2"}, 
{"key3":"value3"}],"deep_nested_array":[{"object_array1": 
[{"key4":"value1"},{"key5":"value2"},{"key6":"value3"}]}, 
{"object_array2":[{"key7":"value4"},{"key8":"value5"}, 
{"key9":"value6"}]}]}

Original Path:null/
token:null
items matching the token:null
paths:
item returningg:null

 recursing:{"array": 
[1,2,3],"boolean":true,"null":null,"number":123,"object": 
{"a":"b","c":"d","e":"f"},"string":"Hello World","object_array": 
[{"key1":"value1"},{"key2":"value2"}, 
{"key3":"value3"}],"deep_nested_array":[{"object_array1": 
[{"key4":"value1"},{"key5":"value2"},{"key6":"value3"}]}, 
{"object_array2":[{"key7":"value4"},{"key8":"value5"}, 
{"key9":"value6"}]}]}

Original Path:object/a/
token:object
items matching the token:{"a":"b","c":"d","e":"f"}
paths:a/
recursing:{"a":"b","c":"d","e":"f"}

Original Path:a/
token:a
items matching the token:"b"
paths:
item returningg:"b"
paths:
item returningg:{"a":"b","c":"d","e":"f"} //Dont need this to be 
                                            returned
recursing:{"array": 
[1,2,3],"boolean":true,"null":null,"number":123,"object": 
{"a":"b","c":"d","e":"f"},"string":"Hello World","object_array": 
[{"key1":"value1"},{"key2":"value2"}, 
{"key3":"value3"}],"deep_nested_array":[{"object_array1": 
[{"key4":"value1"},{"key5":"value2"},{"key6":"value3"}]}, 
{"object_array2":[{"key7":"value4"},{"key8":"value5"}, 
{"key9":"value6"}]}]}

Original Path:object/c/
token:object
items matching the token:{"a":"b","c":"d","e":"f"}
paths:c/
recursing:{"a":"b","c":"d","e":"f"}
Original Path:c/
token:c
items matching the token:"d"
paths:
item returningg:"d"
paths:
item returningg:{"a":"b","c":"d","e":"f"}

recursing:{"array": 
[1,2,3],"boolean":true,"null":null,"number":123,"object": 
{"a":"b","c":"d","e":"f"},"string":"Hello World","object_array": 
[{"key1":"value1"},{"key2":"value2"}, 
{"key3":"value3"}],"deep_nested_array":[{"object_array1": 
[{"key4":"value1"},{"key5":"value2"},{"key6":"value3"}]}, 
{"object_array2":[{"key7":"value4"},{"key8":"value5"}, 
{"key9":"value6"}]}]}

Original Path:object/e/
token:object
items matching the token:{"a":"b","c":"d","e":"f"}
paths:e/
recursing:{"a":"b","c":"d","e":"f"}
Original Path:e/
token:e
items matching the token:"f"
paths:
item returningg:"f"
paths:
item returningg:{"a":"b","c":"d","e":"f"}

recursing:{"array": 
[1,2,3],"boolean":true,"null":null,"number":123,"object": 
{"a":"b","c":"d","e":"f"},"string":"Hello World","object_array": 
[{"key1":"value1"},{"key2":"value2"}, 
{"key3":"value3"}],"deep_nested_array":[{"object_array1": 
[{"key4":"value1"},{"key5":"value2"},{"key6":"value3"}]}, 
{"object_array2":[{"key7":"value4"},{"key8":"value5"}, 
{"key9":"value6"}]}]}

 Original Path:number/
 token:number
 items matching the token:123
 paths:
 item returningg:123
 recursing:{"array": 
 [1,2,3],"boolean":true,"null":null,"number":123,"object": 
 {"a":"b","c":"d","e":"f"},"string":"Hello World","object_array": 
 [{"key1":"value1"},{"key2":"value2"}, 
 {"key3":"value3"}],"deep_nested_array":[{"object_array1": 
 [{"key4":"value1"},{"key5":"value2"},{"key6":"value3"}]}, 
 {"object_array2":[{"key7":"value4"},{"key8":"value5"}, 
 {"key9":"value6"}]}]}

 Original Path:deep_nested_array/object_array1/key4/
 token:deep_nested_array
 items matching the token:[{"object_array1":[{"key4":"value1"}, 
 {"key5":"value2"},{"key6":"value3"}]},{"object_array2":[ 
 {"key7":"value4"},{"key8":"value5"},{"key9":"value6"}]}]

 paths:object_array1/key4/

 recursing:{"object_array1":[{"key4":"value1"},{"key5":"value2"}, 
 {"key6":"value3"}]}
 Original Path:object_array1/key4/
 token:object_array1
 items matching the token:[{"key4":"value1"},{"key5":"value2"}, 
 {"key6":"value3"}]
 paths:key4/
 recursing:{"key4":"value1"}
 Original Path:key4/
 token:key4
 items matching the token:"value1"
 paths:
 item returningg:"value1"

 recursing:{"key5":"value2"}
 Original Path:key4/
 token:key4

 validationjson: 
 /home/mavericks/CLionProjects/validationjson/ 
 include/rapidjson/document.h 
:1137: rapidjson::GenericValue<Encoding, Allocator>& 
 rapidjson::GenericValue<Encoding, Allocator>::operator[](const 
 rapidjson::GenericValue<Encoding, SourceAllocator>&) [with 
 SourceAllocator 
 = rapidjson::MemoryPoolAllocator<>; Encoding = rapidjson::UTF8<>; 
 Allocator = rapidjson::MemoryPoolAllocator<>]: Assertion `false' 
 failed.

This error is because the for loop for the IsArray is continuing with 

令牌为键4,路径为key4 /而不是结束。 帮助解决这个问题将不胜感激。

0 个答案:

没有答案