E2108 不正确使用 typedef 'TJSONObject' 使用 GetValue 路径评估器时出错

E2108 Improper use of typedef 'TJSONObject' Error when using GetValue path evaluator

我无法将路径评估器与 GetValue 一起使用。

情况是这样的:

[{"source":"aaaa","cluster":"1","tokens":[{},{}]}, {"source":"bbbb","cluster":"2","tokens":[{},{}]}]    
TJSONArray *Data = ...;

TJSONObject *obj;
TJSONPair *jpa;

for(int i=0; i<Data->Size(); i++)
{
  obj = (TJSONObject*) Data->Get(i); 
  jpa = obj->GetValue<TJSONObject>("$.tokens");
}

我在这一行遇到错误:

jpa = obj->GetValue<TJSONObject>("$["+IntToStr(i)+"].tokens");

ERROR: E2108 Improper use of typedef 'TJSONObject'

如何正确使用路径求值器?

tokens 字段是数组,不是对象。所以你需要从 GetValue<T>() 请求 TJSONArray 而不是 TJSONObject.

但是,更重要的是,您从 GetValue<T>() 请求的类型必须是 pointer 类型,因为您请求的是 JSON class 类型,而不是像 intString 这样的内置类型。由于您请求的是 TJSONObject 而不是 TJSONObject*,这就是您收到错误的原因。

此外,GetValue<T>() returns 对的 value 部分,而不是对本身。因此,您需要将返回的指针分配给 TJSONArray* 变量(jpaTJSONPair*)。

试试这个:

TJSONArray *Data = ...;

TJSONObject *obj;
TJSONArray *arr;

for(int i = 0; i < Data->Size(); i++)
{
    obj = (TJSONObject*) Data->Get(i);
    arr = obj->GetValue<TJSONArray*>("tokens");
}

或:

TJSONArray *Data = ...;

TJSONArray *arr;

for(int i = 0; i < Data->Size(); i++)
{
    arr = Data->GetValue<TJSONArray*>("$["+IntToStr(i)+"].tokens");
}