C# 调用接口处理返回json数据

项目中处理json需要使用Newtonsoft,访问接口使用RestSharp,先将这两个库添加项目引用,在文件头部进行引用

using Newtonsoft.Json;

using Newtonsoft.Json.Linq;

using RestSharp;

方式一:按数组解析

InfoModel info = new InfoModel();

string hostV = ins.Host;

string urlV = ins.Url;

string stationNameV = ins.StationName;

var client = new RestClient(hostV);

string url = hostV + urlV + stationNameV;

var request = new RestRequest( url , Method.POST);

request.AddHeader("Content-Type", "application/json");

var response = client.Execute(request);

JObject jo = (JObject)JsonConvert.DeserializeObject(response.Content);

string success = jo["success"].ToString();

string msg = jo["msg"].ToString();

string name = jo["name"].ToString();

string tempreture = jo["tempreture"].ToString();

string humidity = jo["humidity"].ToString();

string inputTime = jo["inputTime"].ToString();

info.Success = success;

info.Msg = msg;

if (info.Success == "true")

{

info.Tempreture = tempreture;

// MessageBox.Show(tempreture);

}

方式二:映射转换类

定义类模型InfoModel

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace monitorFlowworkAndSubmit.Model

{

public class InfoModel

{

private string _success;

private string _msg;

private string _id;

private string _stationName ;

private string _tempreture ;

private string _humidity ;

private string _inputTime;

public string Success

{

get { return _success; }

set { _success = value; }

}

public string Msg

{

get { return _msg; }

set { _msg = value; }

}

public string Id

{

get { return _id; }

set { _id = value; }

}

public string StationName

{

get { return _stationName; }

set { _stationName = value; }

}

public string Tempreture

{

get { return _tempreture; }

set { _tempreture = value; }

}

public string Humidity

{

get { return _humidity; }

set { _humidity = value; }

}

public string InputTime

{

get { return _inputTime; }

set { _inputTime = value; }

}

}

}

获取及转换代码

InfoModel info = new InfoModel();

string hostV = ins.Host;

string urlV = ins.Url;

string stationNameV = ins.StationName;

var client = new RestClient(hostV);

string url = hostV + urlV + stationNameV;

var request = new RestRequest( url , Method.POST);

request.AddHeader("Content-Type", "application/json");

var response = client.Execute(request);

info = JsonConvert.DeserializeObject<InfoModel>(response.Content);

相关推荐
脑电信号要分类1 小时前
将多张图片拼接成一个pdf文件输出
pdf·c#·apache
njsgcs1 小时前
c# solidworks 折弯系数检查
开发语言·c#
格林威3 小时前
工业相机图像采集:Grab Timeout 设置建议——拒绝“假死”与“丢帧”的黄金法则
开发语言·人工智能·数码相机·计算机视觉·c#·机器视觉·工业相机
唐青枫3 小时前
C#.NET SignalR + Redis Backplane 深入解析:多节点部署与跨实例消息同步
c#·.net
ChoSeitaku5 小时前
NO.4|protobuf网络版通讯录|httplib|JSON、XML、ProtoBuf对比
xml·json
青衫客366 小时前
浅谈 Java 后端对象映射:从 JSON → VO → Entity 的原理与实践
java·json
qqxhb14 小时前
11|结构化输出:为什么 JSON 能让系统更稳定
json·ai编程·结构化·规范模板
FL162386312916 小时前
[C#][winform]segment-anything分割万物部署onnx模型一键抠图演示
开发语言·c#
love530love17 小时前
OpenClaw 手机直连配置全流程
人工智能·windows·python·智能手机·c#·agent·openclaw
bcbobo21cn19 小时前
C# byte类型和byte数组的使用
开发语言·c#·字节数组·byte类型