Ads 468x60px

##EasyReadMore##

30 5月, 2016

Json-c 分解再合併

一個將 json 分解再合併的範例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <json/json.h>
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)

#define TOPIC "Rule"
#define CMD  "cmd"

#define CMD_SET_RULE 4
#define CMD_ARAM  8
#define CMD_AUTO_SEND 9

#define OPTION   "op"
#define RID    "rule_id"
#define UID    "uid" //unique id
#define CUID   "c_uid" //Json_ConnectionUniqueId
#define SID    "sid" //service id
#define VALUE   "v"
#define RULE   "rule"
#define SETTYPE   "set_t"
#define ACTION   "action"
#define RC    "rc"
#define OK    0
#define ERROR   1

#define RULE_ADD  1
#define RULE_UPDATE  2
#define RULE_DELETE  3
#define RULE_QUERY  4

#include <sqlite3.h>

int message_paser(char *topic,char *message){
 
 struct json_object *new_obj;
 struct json_object *jobj,*jtmp,*jarrayobj,*judege_list,*jaction,*jrc;
 struct array_list *rule_array;
 int pageCount,rc = 0;
 
 printf("billy[%s:%d,%s] message = %s\n", __FILENAME__, __LINE__, __FUNCTION__,message);
 if(strcmp(topic,TOPIC) != 0 ){
  printf("billy[%s:%d,%s] topic error = %s\n", __FILENAME__, __LINE__, __FUNCTION__,topic);
  return -1;
 }
 jobj = json_tokener_parse(message);
 if(jobj != NULL){
  jtmp = json_object_object_get(jobj,CMD);
  if(jtmp != NULL){
   int cmd_num;
   int op,rule_num,action_num,i = 0;
   const char *cuid,*rule_id,*uid,*sid,*set_type,*value,*cmd;
   
   cuid = json_object_get_string(json_object_object_get(jobj,CUID));
   cmd = json_object_get_string(jtmp);
   printf("billy[%s:%d,%s] cmd = %s cuid = %s\n", __FILENAME__, __LINE__, __FUNCTION__,cmd,cuid);
   cmd_num = strtol(cmd,NULL,0);
   printf("billy[%s:%d,%s] cmd_num = %d\n", __FILENAME__, __LINE__, __FUNCTION__,cmd_num);
   
   switch(cmd_num){
    case CMD_SET_RULE:
     jtmp = json_object_object_get(jobj,OPTION);
     op = json_object_get_int(jtmp);
     printf("billy[%s:%d,%s] option = %d\n", __FILENAME__, __LINE__, __FUNCTION__,op);
     switch(op){
      case RULE_ADD:
       jtmp = json_object_object_get(jobj,RID);
       rule_id = json_object_get_string(jtmp);
       printf("billy[%s:%d,%s] rule_id = %s\n", __FILENAME__, __LINE__, __FUNCTION__,rule_id);
       
       //findDevice
       jtmp = json_object_object_get(jobj,RULE);
       rule_num = json_object_array_length(jtmp);
       printf("billy[%s:%d,%s] rule_num = %d\n", __FILENAME__, __LINE__, __FUNCTION__,rule_num);
       
       judege_list = json_object_new_object();
       
       //get rule array
       for(i = 0;i < rule_num;i++ ){
        //get element
        jarrayobj = json_object_array_get_idx(jtmp,i);
        uid = json_object_get_string(json_object_object_get(jarrayobj,UID));
        sid = json_object_get_string(json_object_object_get(jarrayobj,SID));
        set_type = json_object_get_string(json_object_object_get(jarrayobj,SETTYPE));
        value = json_object_get_string(json_object_object_get(jarrayobj,VALUE));
        printf("billy[%s:%d,%s] uid = %s sid = %s set_type = %s value = %s\n", __FILENAME__, __LINE__, __FUNCTION__,uid,sid,set_type,value);
        
        //write to judege list DB
        json_object_object_add(judege_list,UID,json_object_new_string(uid));
        json_object_object_add(judege_list,SID,json_object_new_string(sid));
        json_object_object_add(judege_list,SETTYPE,json_object_new_string(set_type));
        json_object_object_add(judege_list,VALUE,json_object_new_string(value));
        printf("billy[%s:%d,%s] judege list = %s\n", __FILENAME__, __LINE__, __FUNCTION__,json_object_to_json_string(judege_list));
       }
       
       //get action array
       jtmp = json_object_object_get(jobj,ACTION);
       action_num = json_object_array_length(jtmp);
       printf("billy[%s:%d,%s] action_num = %d\n", __FILENAME__, __LINE__, __FUNCTION__,action_num);
       for(i = 0;i < action_num;i++){
        jaction = json_object_array_get_idx(jtmp,i);
        json_object_object_add(judege_list,ACTION,jaction);
        printf("billy[%s:%d,%s] jaction = %s\n", __FILENAME__, __LINE__, __FUNCTION__,json_object_to_json_string(jaction));
       }
       
       //return rc state
       jrc = json_object_new_object();
       json_object_object_add(jrc,CUID,json_object_new_string(cuid));
       json_object_object_add(jrc,RC,json_object_new_int(OK));
       printf("billy[%s:%d,%s] return = %s\n", __FILENAME__, __LINE__, __FUNCTION__,json_object_to_json_string(jrc));
       
       break;
      case RULE_UPDATE:
       break;
      case RULE_DELETE:
       break;
      case RULE_QUERY:
       break;
      default:
       printf("billy[%s:%d,%s] other option = %d\n", __FILENAME__, __LINE__, __FUNCTION__,op);
       break;
     }
     break;
    case CMD_ARAM:
     break;
    case CMD_AUTO_SEND:
     break;
    default:
     printf("billy[%s:%d,%s] other cmd %d\n", __FILENAME__, __LINE__, __FUNCTION__,cmd_num);
     break;
   }
  }
  else
   printf("billy[%s:%d,%s] error\n", __FILENAME__, __LINE__, __FUNCTION__);
  
 }
 else
  printf("billy[%s:%d,%s] json error\n", __FILENAME__, __LINE__, __FUNCTION__);
 
 new_obj = json_tokener_parse(
     "{ \"glossary\": { \"title\": \"example glossary\", \"pageCount\": 100, \"GlossDiv\": { \"title\": \"S\", \"GlossList\": [ { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": \"A meta-markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\", \"markup\"] } ] } } }");

 return 0;
}

int main(int argc, char **argv) {
 char *message = "{\"c_uid\":\"123\",\"cmd\":\"04\",\"op\":\"1\",\"rule_id\":\"a1\",\"rule\":[{\"uid\":\"ZZ0000000000000001\",\"sid\":\"1\",\"set_t\":\"2\",\"v\":\"30.1\"},{\"uid\":\"ZZ0000000000000001\",\"sid\":\"2\",\"set_t\":\"2\",\"v\":\"80\"}],\"action\":[{\"uid\":\"3\",\"sid\":\"1\",\"v\":\"1\"}]}";
 message_paser("Rule",message);
 return 0;
}

0 意見:

張貼留言

 
Blogger Templates