首页 > 其他分享 >利用cJSON库解析http返回参数

利用cJSON库解析http返回参数

时间:2024-06-12 19:12:46浏览次数:8  
标签:recvbuf 解析 http socket cJSON destaddr printf include

利用cJSON库解析http返回参数

在C语言中,使用cJSON库来解析HTTP响应返回的JSON参数通常涉及几个步骤:

  1. 发送HTTP请求(可以利用soket库进行http请求)
  2. 接收HTTP响应的数据
  3. 将接收到的数据作为字符串传递给cJSON库进行解析
  4. 使用cJSON API访问和提取JSON数据

以下是一个示例,展示了如何使用soket向特定的API接口发送HTTP GET请求,并使用cJSON解析返回的JSON数据:

首先,确保你已经安装了socket和cJSON库

/*******************************************************************
*
*	file name:	get_tenphone.c
*	author	 :  [email protected]
*	date	 :  2024/06/11
*	function :  利用socket库对API接口发送http请求,并解析返回的参数
* 	note	 :  None
*
*	CopyRight (c)  2023-2024   [email protected]  All Right Reseverd 
*
* *****************************************************************/
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <errno.h>
#include <string.h>
#include "cJSON.h"

#define APIKEY  "******************"//以两宏定义参数替换成自己的参数
#define PHONE   "*************"	 
int main(int argc, char const *argv[])
{
	
	int tcp_socket = socket(AF_INET, SOCK_STREAM, 0);

	
	struct sockaddr_in  destaddr;
	destaddr.sin_family = AF_INET; 
	destaddr.sin_port   = htons(80); 
	destaddr.sin_addr.s_addr = inet_addr("203.107.54.210"); 
	
	int ret = connect(tcp_socket,(struct sockaddr *)&destaddr, sizeof(destaddr));
	if (-1 == ret)
	{
		perror("connect server error");
		exit(1);
	}

	printf("connect success!\n");

	 char recvbuf[1024] ={0};
	 sprintf(recvbuf, "GET http://apis.juhe.cn/mobile/get?phone=%s&key=%s HTTP/1.1\r\n"
                     "Host:apis.juhe.cn\r\n"
                    "Content-Type: application/x-www-form-urlencoded\r\n"
	 			            "\r\n\r\n"
                 ,PHONE,APIKEY);


	
  send(tcp_socket,recvbuf,strlen(recvbuf),0);
 
	char *deal;
	while(1)
	{
		recv(tcp_socket,recvbuf,1024,0);  
		printf("%s\n",recvbuf);

		deal=strstr(recvbuf,"{");
	    printf("%s\n",deal);
		
		break;
	}
     
	cJSON *root = cJSON_Parse(deal);
	printf("%s\n",cJSON_Print(root)); 

	cJSON * obj1 = cJSON_GetObjectItem(root,"result");
	
	 cJSON *province = cJSON_GetObjectItem(obj1,"province");
	 printf("地区:%s",province->valuestring);
	
	cJSON *city = cJSON_GetObjectItem(obj1,"city");
	 printf("%s\n",city->valuestring);

	cJSON *company = cJSON_GetObjectItem(obj1,"company");
	 printf("运营商:%s\n",company->valuestring); 

	return 0;
}

如果代码用法有什么问题,请将问题发至网易邮箱 [email protected],作者将及时改正,欢迎与各位老爷交流讨论。

麻烦三连加关注!!!!

比心

标签:recvbuf,解析,http,socket,cJSON,destaddr,printf,include
From: https://www.cnblogs.com/zkbklink/p/18244531

相关文章

  • 从零手写实现 nginx-19-HTTP CORS(Cross-Origin Resource Sharing,跨源资源共享)介绍+解
    前言大家好,我是老马。很高兴遇到你。我们为java开发者实现了java版本的nginxhttps://github.com/houbb/nginx4j如果你想知道servlet如何处理的,可以参考我的另一个项目:手写从零实现简易版tomcatminicat手写nginx系列如果你对nginx原理感兴趣,可以阅读:从零......
  • 深入解析 JMeter TPS 测试:从理论到实践
    前言作为一名测试工程师,理解并能够准确测量每秒事务数(TransactionsPerSecond,TPS)是确保系统性能的关键指标之一。本文将详细介绍如何使用ApacheJMeter进行TPS测试,包括理论基础、配置步骤和结果分析。什么是TPS?TPS(TransactionsPerSecond)表示系统每秒能够处理的事务......
  • 掌握JMeter:深入解析如何提取和利用JSON数据
    前言ApacheJMeter不仅是一个功能强大的性能测试工具,它还可以用于提取和处理响应中的数据。对于现代Web应用,JSON(JavaScriptObjectNotation)已经成为主要的数据交换格式。本文将详细介绍如何在JMeter中提取JSON数据,并将其用于后续的请求或断言。环境准备在开始之前,请确保已经......
  • c#编写一个简单的http服务器
    C#代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Net;usingSystem.IO;namespaceConsoleApplication1{classProgram{staticvoidMain(string[]args){usin......
  • Maven无法下载依赖之 maven-default-http-blocker (http://0.0.0.0/): Blocked mirror
    问题:maven-default-http-blocker(http://0.0.0.0/):Blockedmirrorforrepositories:原因:名称为maven-default-http-blocker的拦截器拦截了库的镜像。场景:在企业内部架设了一个Nexus的私有库,库的地址是:http://hostname:8081/repository/maven-public/,于是在项目中需要从这......
  • go http请求
    funcSend(notificationmodel.Notification,defaultRobotstring)(errerror){markdown,robotURL,err:=transformer.TransformToMarkdown(notification)iferr!=nil{return}data,err:=json.Marshal(markdown)iferr!=nil{......
  • node使用http-proxy-middleware做代理,解决跨域问题
    constexpress=require('express')consthistory=require('connect-history-api-fallback')const{createProxyMiddleware}=require('http-proxy-middleware')letapp=express()app.use(history())////匹配api开头的请求,实际转发的请求保a......
  • 域名解析很重要吗?
    互联网已经成为我们生活和工作中不可或缺的一部分。当我们畅游在网络世界时,经常会听到一个术语——域名解析。一、域名是什么?域名,简单来说,就是互联网上识别和定位计算机的层次结构式的字符标识。它就如同是网络世界里的“地址”。我们可以将其类比为现实世界中的地址。在现实中......
  • http 响应415状态码
    http响应415状态码现象$bashuat-gray-id.sh{"timestamp":"2024-06-1213:55:25","status":415,"error":"UnsupportedMediaType","message":"","path":"/ecommerce"}脚本内容$cat......
  • AIGC绘画设计:深入浅出完整解析Stable Diffusion(SD)核心基础知识
    心血来潮再加上想要督促自己,所以决定开始搞AI绘画技术的分享。如果觉得我写的好,又很久没更新,请多点我,因为可能我的懒癌又犯了,哈哈哈哈哈......没有本人帅,十分之一都没有不多废话,切入主题。学一门技术什么最难?当然是入门最难!从0到1,远远比从1到100要难一百倍,为啥?因为你之前......