Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15698
  • 博文数量: 7
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 25
  • 用 户 组: 普通用户
  • 注册时间: 2015-11-05 15:48
个人简介

蜗牛

文章分类

全部博文(7)

文章存档

2015年(7)

我的朋友
最近访客

分类: 嵌入式

2015-11-19 20:12:27

一、Json转string    

main.c

  1. #include <string.h>
  2. #include "js.h"

  3. //{"type":1,"user":{"username":"xxx";"password":"1234"},"result":1}
  4. int main(void)
  5. {
  6.     struct feedback fd;    
  7.     fd.type=1;
  8.     fd.result=1;
  9.     fd.u.username="XXX";
  10.     fd.u.password="1234";
  11.     //memcpy(fd.u,&u1,sizeof(fd.u));
  12.     
  13.     json_string(&fd);

  14.     return 0;
  15. }

js.h

  1. #ifndef __JS_H__
  2. #define __JS_H__

  3. #include <stdio.h>
  4. #include "/usr/local/include/json/json.h"

  5. struct user{
  6.     char *username;
  7.     char *password;
  8. };

  9. struct feedback{
  10.     int type;
  11.     int result;
  12.     struct user u;
  13. };

  14. //struct feedback fd;
  15. extern void json_string(struct feedback *f);


  16. #endif

json_string.c

  1. //{"type":1,"user":{"username":"xxx";"password":"1234"},"result":1}

  2. #include "js.h"

  3. void json_string(struct feedback *f)
  4. {
  5.     struct json_object *fat,*usr,*json_usr,*json_type,*json_username,*json_password,*json_result;
  6.     usr=json_object_new_object();
  7.         json_username= json_object_new_string(f->u.username);
  8.     json_password= json_object_new_string(f->u.password);
  9.         json_object_object_add(usr,"username",json_username);
  10.         json_object_object_add(usr,"password",json_password);

  11.     fat=json_object_new_object();
  12.     json_type=json_object_new_int(f->type);
  13.     json_result=json_object_new_int(f->result);
  14.     json_object_object_add(fat,"type",json_type);
  15.     json_object_object_add(fat,"user",usr);
  16.     json_object_object_add(fat,"result",json_result);

  17.     const char * json_string = json_object_to_json_string(fat);
  18.     printf("%s\n",json_string);
  19. }
二、string转Json

mai.c

  1. #include <stdio.h>

  2. int main(void)
  3. {
  4.     char *msg="{\"intent\":\"ms\",\"type\":1,\"user\":{\"username\":\"xxx\",\"password\":\"1234\"}}";
  5.     string_json(msg);
  6.     return 0;
  7. }

string_json.c

  1. #include <stdio.h>
  2. #include </usr/local/include/json/json.h>

  3. //{"intent":"ms","type":1,"user":{"username":"xxx";"password":"1234"}}
  4. void string_json(char *msg)
  5. {    
  6.     printf("%s\n",msg);
  7.         
  8.     struct json_object *fat,*json_intent,*json_type,*json_user,*json_username,*json_password;
  9.     char *intent,*username,*password;
  10.     int type;

  11.     fat=json_tokener_parse(msg);
  12.     json_intent=json_object_object_get(fat,"intent");
  13.     json_type=json_object_object_get(fat,"type");
  14.     json_user=json_object_object_get(fat,"user");
  15.     

  16.     json_username=json_object_object_get(json_user,"username");
  17.     json_password=json_object_object_get(json_user,"password");

  18.     intent=(char *)json_object_get_string(json_intent);
  19.     type=json_object_get_int(json_type);
  20.     username=(char *)json_object_get_string(json_username);
  21.     password=(char *)json_object_get_string(json_password);
  22.     
  23.     printf("%s\t%d\t%s\t%s\n",intent,type,username,password);

  24. }


阅读(1502) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~