gSOAP:如何比较C中的两个值结构(带有JSON内容)?

问题描述:

我正在调用Java-webservice的客户端应用程序中使用gSOAP(C语言)。我正在使用功能json_call()。 我有一个请求结构充满了JSON输入数据,我得到一个响应结构充满了来自Java服务的JSON输出数据。两种JSON通常具有相同的结构,但可以具有更多,更少或更改的元素。gSOAP:如何比较C中的两个值结构(带有JSON内容)?

我现在的任务是找出响应与请求不同的元素。主元件是一个大阵列有很多成员,如:

{ 
    "objects": [ 
    { 
     "@id": "OBJ00001", 
     "name": "value", 
     ... 
    }, 
    { 
     "@id": "OBJ00002", 
     "number": 123, 
     ... 
    }, 
    ... 
    ] 
} 

我可以识别相同种类与@id字段的任何对象。

It's简单的东西,如迭代的objects数组:

for(i = 0; i < has_size(value_at(response, "objects")); i++) 

但随后I'missing一个函数,它可以在请求和响应进行比较以相同的@id成员(“对象”)。像“findMemberWithSameField”,然后选择“等于”(这两者不存在!):

struct member *currentMemberInResponse = NULL; 
struct member *memberWithSameField  = NULL; 

for(i = 0; i < has_size(value_at(response, "objects")); i++) 
{ 
    /* get the current member out of the response array */ 
    currentMemberInResponse = nth_value(value_at(response, "objects"), i); 

    /* Find member/object with same @id in request */ 
    memberWithSameField = findMemberWithSameField(value_at(request, "objects"), currentMemberInResponse , "@id")); 

    /* equal is true if all fields are the same */ 
    if(equal(currentMemberInResponse, memberWithSameField)) 
    { 
     /* Do nothing, because nothing changed */ 
    } 
    else 
    { 
     /* Do something */ 
    } 
} 

这项任务的任何想法?否则,我必须写我自己的“findMemberWithSameField”和“euqal”。

亲切的问候丹尼尔

+0

这里看看https://github.com/DaveGamble/cJSON它有jSON补丁支持 –

的JSON C++ API定义operator==递归比较两个对象。最新版本的55年2月8日作品(我测试过)来比较两个JSON对象,其中operator==调用下面的函数:

bool json_eqv(const value& x, const value& y) 
{ 
    ... 
    switch (x.__type) 
    { 
    ... 
    case SOAP_TYPE__struct: 
     if (x.size() != y.size()) 
     return false; 
     else 
     { 
     const _struct& s = x; 
     const _struct& t = y; 
     for (_struct::iterator i = s.begin(); i != s.end(); ++i) 
     { 
      _struct::iterator j; 
      for (j = t.begin(); j != t.end(); ++j) 
      if (!strcmp(i.name(), j.name())) 
       break; 
      if (j == t.end() || *i != *j) 
      return false; 
     } 
     return true; 
     } 

这可以被改写到C的东西,如:

int json_eqv(const value *x, const value *y) 
{ 
    if (x->__type != y->__type && 
     (x->__type != SOAP_TYPE__i4 || y->__type != SOAP_TYPE__int) && 
     (x->__type != SOAP_TYPE__int || y->__type != SOAP_TYPE__i4)) 
    return false; 
    switch (x->__type) 
    { 
    case SOAP_TYPE__boolean: 
    case SOAP_TYPE__i4: 
    case SOAP_TYPE__int: 
     return int_of(x) == int_of(y); 
    case SOAP_TYPE__double: 
     return double_of(x) == double_of(y); 
    case SOAP_TYPE__string: 
    case SOAP_TYPE__dateTime_DOTiso8601: 
     return !strcmp(string_of(x), string_of(y)); 
    case SOAP_TYPE__struct: 
     if (has_size(x) != has_size(y)) 
     return 0; 
     else 
     { 
     size_t i, j; 
     for (i = 0; i < has_size(x); ++i) 
     { 
      for (j = 0; j < has_size(y); ++j) 
      if (!strcmp(nth_member(x, i), nth_member(y, j)) 
       break; 
      if (j == has_size(y) || !json_eqv(nth_value(x, i), nth_value(y, j)) 
      return 0; 
     } 
     return 1; 
     } 
    case SOAP_TYPE__array: 
     if (has_size(x) != has_size(y)) 
     return 0; 
     else 
     { 
     int i; 
     for (i = 0 ; i < has_size(x); ++i) 
      if (!json_eqv(nth_nth(x, i), nth_nth(y, i)) 
      return 0; 
     return 1; 
     } 
    default: 
     return 0; 
    } 
}