2017-09-22 1 views
0

Java 응용 프로그램을 호출하는 클라이언트 응용 프로그램에 gSOAP (C 언어)을 사용하고 있습니다. 나는 json_call() 함수를 사용하고있다. 요청 구조체에 JSON 입력 데이터가 채워져 있고 응답 구조체에 Java 서비스의 JSON 출력 데이터가 채워져 있습니다. 두 JSON은 일반적으로 동일한 구조를 갖지만 더 많거나 적은 요소 또는 변경된 요소를 가질 수 있습니다.gSOAP : C에서 두 개의 value-struct (JSON-content 포함)를 비교하는 방법은 무엇입니까?

이제 내 과제는 어떤 요소가 요청과 다른지 찾아 보는 것입니다.

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

내가 @id 필드와 같은 종류의 물체를 식별 할 수 있습니다 주요 요소는 같은 회원의 많은 대형 배열입니다.

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

그러나 다음 요청에 응답하여 동일한 @id와 부재 ("사물")을 비교할 수있는 기능을 I'missing :

같은 것을하여 objects 배열을 반복하는 단순한 키우면. (모두가 존재하지 않는 !) "동일한"다음 "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"을 써야합니다.

종류는 JSON C++ API는 재귀 적으로 두 개체를 비교하는 operator==을 정의 다니엘

+0

https://github.com/DaveGamble/cJSON에서 jSON 패치를 지원합니다. –

답변

0

을 간주한다. 최신 버전 2.8.55 작품 (내가 테스트 한) operator==는 다음과 같은 함수를 호출 할 경우,이 JSON 객체를 비교 :

이 같은 뭔가를 C로 다시 쓸 수
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; 
     } 

: 여기

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; 
    } 
} 
관련 문제