C#:Newtonsoft.Json将json字符串转换为实体对象

 

1.Json字符串:

复制代码
{
  "OptTypeList": [
    [
      {
        "address": "宁波市鄞州区*****",
        "divisionName": "********",
        "tel": "010-12345678",
        "divisionId": "813402",
        "psummertime": "08:00",
        "pwintertime": "18:00",
        "streetId": "330212018"
      },
      {
        "address": "宁波市鄞州区********",
        "divisionName": "********",
        "divisionId": "811904",
        "streetId": "330212027"
      }
    ]
  ],
  "_TaskId": "53506a7943354748a1affdb9f635fb86",
  "_Return": "000000",
  "_ParentTaskId": "1f2bbefca759436a94017f55eed8512e"
}
复制代码

2.打开网页,复制Json字符串,生成实体类

JSON转C#实体类-BeJSON.com

3.将生成的实体类加入项目代码;

4.引入Newtonsoft.Json.dll

Root root = JsonConvert.DeserializeObject<Root>(jsonString);

5、测试:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Collections;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
using System.Web;
//using System.Web.Extensions;
using System.Web.Services;
using sdk_cop;
using System.Runtime.Serialization.Json;
using Newtonsoft.Json.Linq;
using System.Data;
using System.Xml;
using System.Web.Script.Serialization;
using Newtonsoft.Json;

namespace sdk_cop
{
[ClassInterface(ClassInterfaceType.None)]
public class PayQrcodeScannedPay
{
protected static String APP_ID = "10000000000004096797";
protected static String MY_PRIVATE_KEY = @"MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDvDdMai6cUYeayz0weCEeCGHfzt6HWin7p008K+Zm8bpXdjpCMo75HnzmOfyrVKhz7D0f+967P6OTA51hE55ZL2fPHR/TFA08omaOGQYyrPxo6XQtXM4pCbNuIsiWJskC18AeWKqkxK6K0wGZQFdPe132SMM4l6hyIy44v1t6WshoDjRHS7+1fe2ICYDG7mggcaGN/X8KOoRRbZqP9trEGOxA1k8py7g1eUypNdHeK6ingZyswpVxQWR43WgF3GXYMTXf/LISm9pXod6u4XXs4pGzdKSsQhn5Ly0ys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DMuY29tLmNuMB0GA1UdDgQWBBSwq0L7EIadh6+VMaRQQU4miM7y1TANBgkqhkiG9w0BAQUFAAOCAQEACJVJnhovUgwL5H14dJ068vNZkve55n+Ew9doaFyLubdUEkdhiXwxftrKiizW+D2ZqgTuDQr+T9KmyVEMdzaiNhxaBjEs2dxmg7WVcHd85VZTD6QbCJDLabrj6RRL5aLCPTSS/xA23ieA0/HS59bl70nYLKj1zUL2WtlOExv+rAQ1bQuuFhj5GMioEnLSpwz/0KrIHs2tu3P0UnK1xoQZEwEBP06g76vgmzkykK+jIjm0ubDirGSnxIk964/TeSyapePZK0bUEOb4AkrBwDE93x0QHZUOONy531p9FAEOcY0Pyr8Ukmz+Z6kmCVEltlDMYTsV9dG36cuvEuyFcToDxg==";
protected static String key = "C:\\Users\\kfzx-zhoux\\Desktop\\xinyinqi1.key";
protected static string retuenParamOut = "[{\"response_biz_content\":{\"return_code\":\"0\",\"return_msg\":\"\",\"pay_status\":\"1\",\"cust_id\":\"0200000000201\",\"card_no\":\"622************2260\",\"total_amt\":\"1\",\"out_trade_no\":\"134324324\",\"order_id\":\"1111111111111111022233334444444\",\"pay_time\":\"20190321\",\"bank_name\":\"工商银行\",\"channel\":\"91\",\"tp_cust_id\":\"11111111\",\"trx_ser_no\":\"12345678901234567890123\",\"tp_order_id\":\"111\",\"buyer_logon_id\":\"ali1111\",\"sub_open_id\":\"wx1111\",\"bank_type\":\"wx***\",\"promotion_detail\":[{\"promotion_id\":\"10516945010\",\"name\":\"单品券\",\"scope\":\"SINGLE\",\"type\":\"DISCOUNT\",\"amount\":101,\"activity_id\":\"10056495\",\"wxpay_contribute\":0,\"merchant_contribute\":101,\"other_contribute\":0,\"goods_detail\":[{\"goods_id\":\"10000002\",\"quantity\":1,\"price\":201,\"discount_amount\":101,\"goods_remark\":\"\"}]}],\"tp_user_id\":\"2020101231\"},\"sign\":\"Iwl2q0PM6092JXPnacx+EhfKoHsPUBCcKvQ9D4C95Zg2BwYrBPz0z6a9WKlBEDwxhpXIfxN96iV8yffAAYJXqumc34CSoi8R9STWRcOjGUTWTVmbTR6rDMAsPYIAAkm/W2cO16z01ah/wLW+HEf3Q2ByiVwHB7WDWQfLveWUKqI=\"}]";

public string test(DataTable dsInterNet)
{
StringBuilder strMessage = new StringBuilder("微信扫码:PayQrcodeScannedPay:");
#region 入参
string YLJGBM = dsInterNet.Rows[0]["YLJGBM"].ToString().Trim(); //医疗机构编码,支付平台提供
string PERSONALNO = dsInterNet.Rows[0]["PERSONALNO"].ToString().Trim(); //个人编号
string CARDTYPE = dsInterNet.Rows[0]["CARDTYPE"].ToString().Trim(); //卡片类型{10:社保卡,20:就诊卡}
string SCARDNO = dsInterNet.Rows[0]["SCARDNO"].ToString().Trim(); //社保卡号
string JZKH = dsInterNet.Rows[0]["JZKH"].ToString().Trim(); //持卡人在该机构 HIS 系统内关联的就诊卡号
string NAME = dsInterNet.Rows[0]["NAME"].ToString().Trim(); //姓名
string IDCARD = dsInterNet.Rows[0]["IDCARD"].ToString().Trim(); //身份证号
string JSTYPE = dsInterNet.Rows[0]["JSTYPE"].ToString().Trim(); //结算类型
string YHJSLSH = dsInterNet.Rows[0]["YHJSLSH"].ToString().Trim(); //HIS 结算流水号
string XZLX = dsInterNet.Rows[0]["XZLX"].ToString().Trim(); //险种类型 0 职工,1 居民,便于后期对账
string JZH = dsInterNet.Rows[0]["JZH"].ToString().Trim(); //就诊号/门诊号
string DJH = dsInterNet.Rows[0]["DJH"].ToString().Trim(); //单据号
string JSDATE = dsInterNet.Rows[0]["JSDATE"].ToString().Trim(); //结算日期
string YYFZE = dsInterNet.Rows[0]["YYFZE"].ToString().Trim(); //医药费总额
string YBTCJE = dsInterNet.Rows[0]["YBTCJE"].ToString().Trim(); //医保统筹金额
string YBGRZHYE = dsInterNet.Rows[0]["YBGRZHYE"].ToString().Trim(); //医保个人账户余额
string YBGRZHZF = dsInterNet.Rows[0]["YBGRZHZF"].ToString().Trim(); //医保个人账户支付
string JYJE = dsInterNet.Rows[0]["JYJE"].ToString().Trim(); //交易金额
string OPTYPE = dsInterNet.Rows[0]["OPTYPE"].ToString().Trim(); //业务类型
string MERID = dsInterNet.Rows[0]["MERID"].ToString().Trim(); //商户号
string PAYAUTHCODE = dsInterNet.Rows[0]["PAYAUTHCODE"].ToString().Trim(); //支付授权码,扫顾客的支付码获取
string PAYTYPE = dsInterNet.Rows[0]["PAYTYPE"].ToString().Trim(); //指定一种支付方式时,可设置该标签:(1 银联,2 现金,3 银行,12 扫码支付,21 刷卡支付, 7 支付宝代扣)
string FACETYPE = dsInterNet.Rows[0]["FACETYPE"].ToString().Trim(); //刷脸支付方式(4 微信,5 支付宝)
string JSFS = dsInterNet.Rows[0]["JSFS"].ToString().Trim(); //结算方式(1 银联,2 保留,3 银行,4 微信扫码,41 微信刷卡支付,5 支付宝扫码,51 支付宝刷卡支付(条码支付),6 云闪付,7 支付宝代扣,11 银联 pos)
string QTFYBZ = dsInterNet.Rows[0]["QTFYBZ"].ToString().Trim(); //其他费用备注
string OPDATE = dsInterNet.Rows[0]["OPDATE"].ToString().Trim(); //操作日期
string OPERNO = dsInterNet.Rows[0]["OPERNO"].ToString().Trim(); //操作员编号
string OPERNAME = dsInterNet.Rows[0]["OPERNAME"].ToString().Trim(); //操作员姓名
#endregion

string rMessage = "PayQrcodeScannedPay:true";

//签名类型为RSA2时,需传入appid,私钥和网关公钥,签名类型使用定值IcbcConstants.SIGN_TYPE_RSA2,其他参数使用缺省值
DefaultIcbcClient client = new DefaultIcbcClient(APP_ID, IcbcConstants.SIGN_TYPE_RSA, MY_PRIVATE_KEY, APIGW_PUBLIC_KEY);
PayQrcodeScannedPayRequestV5 request = new PayQrcodeScannedPayRequestV5();
//4、根据测试环境和生产环境替换相应ip和端口
request.setServiceUrl("http://ip:port/api/mybank/pay/qrcode/scanned/pay/V5");
//5、请对照接口文档用bizContent.setxxx()方法对业务上送数据进行赋值
PayQrcodeScannedPayRequestV5.PayQrcodeScannedPayRequestV5Biz bizContent = new PayQrcodeScannedPayRequestV5.PayQrcodeScannedPayRequestV5Biz();
bizContent.setMerId("020*****460001"); //商户编号
bizContent.setQrCode(PAYAUTHCODE); //付款码
bizContent.setOutTradeNo(YHJSLSH); //外部订单号
bizContent.setTradeDate(JSDATE.Substring(0, 8)); //交易日期 格式:YYYYMMDD
bizContent.setTradeTime(JSDATE.Substring(8)); //交易时间hhmmss
bizContent.setAttach(QTFYBZ); //商户附加信息
bizContent.setOrderAmt((Convert.ToDecimal(JYJE) * 100).ToString()); //交易金额
bizContent.setSubAppId("wxfb72f1a7d061d631"); //子商户应用号
bizContent.setGoodsTag(""); //优惠标志
//JSONArray goodsDetailArray = new JSONArray();
//JSONObject gd1 = new JSONObject();
//gd1.put("goods_id", "123456");
//gd1.put("goods_name", "FOOD");
//gd1.put("quantity", 1);
//gd1.put("price", 100);
//goodsDetailArray.add(gd1);
bizContent.setGoodsDetail("");//单品优惠活动
bizContent.setSubject("预缴金存入"); //订单标题,128字节长度
bizContent.setLocation("+37.12/-121.213"); //终端实时经纬度信息
bizContent.setNetwork_license("P3100"); //终端入网认证编号
bizContent.setDevice_type("01"); //设备类型
bizContent.setSerial_num(""); //终端序列号
bizContent.setDevice_id(""); //终端设备号
bizContent.setEncrypt_rand_num(""); //加密随机因子
bizContent.setSecret_text(""); //密文数据
bizContent.setApp_version(""); //应用程序版本号
bizContent.setDevice_ip(""); //商户端设备IP
bizContent.setMobile_country_cd("460"); //移动国家代码
bizContent.setMobile_net_num(""); //移动网络号码
bizContent.setIcc_id(""); //ICCID
bizContent.setLocation_cd1(""); //位置区域码1
bizContent.setLbs_num1(""); //基站编号1
bizContent.setLbs_signal1(""); //基站信号1
bizContent.setLocation_cd2(""); //位置区域码2
bizContent.setLbs_num2(""); //基站编号2
bizContent.setLbs_signal2(""); //基站信号2
bizContent.setLocation_cd3(""); //位置区域码3
bizContent.setLbs_num3(""); //基站编号3
bizContent.setLbs_signal3(""); //基站信号3
bizContent.setTelecom_sys_id(""); //电信系统识别码
bizContent.setTelecom_net_id(""); //电信网络识别码
bizContent.setTelecom_lbs(""); //电信基站
bizContent.setTelecom_lbs_signal(""); //电信基站信号
request.setBizContent(bizContent);
PayQrcodeScannedPayResponseV5 response;
try
{
string strJson = retuenParamOut;
var strDic= JsonToFun.Tabulate(strJson);
var strBizContent = JsonToFun.Tabulate(string.Format("[{0}]", strDic[0]["response_biz_content"]));
Response_biz_content response_Biz_Content = JsonConvert.DeserializeObject<Response_biz_content>(strDic[0]["response_biz_content"]);
//JavaScriptSerializer js = new JavaScriptSerializer();
//Response_biz_content user = js.Deserialize<Response_biz_content>(string.Format("{0}", strDic[0]["response_biz_content"]));
var return_code = strBizContent[0]["return_code"];
var return_msg= strBizContent[0]["return_msg"];
if (response_Biz_Content.return_code.Equals("0"))
{

}
else

{
strMessage.AppendLine().AppendFormat("return_code:{0};return_msg:{1};", return_code, return_msg);
}

//response = (PayQrcodeScannedPayResponseV5)client.execute(request, "msgId3");//msgId消息通讯唯一编号,要求每次调用独立生成,APP级唯一
//if (response.getReturnCode() == 0)
//{

// // 6、业务成功处理,请根据接口文档用response.getxxx()获取同步返回的业务数据
// //System.out.println("response:" + JSON.toJSONString(response));
// //System.out.println("ReturnCode:"+response.getReturnCode());
//}
//else
//{
// // 失败
// //System.out.println("ReturnCode:"+response.getReturnCode());
// //System.out.println("ReturnMsg:"+response.getReturnMsg());
//}
}
catch (Exception ex)////Exception ex
{
strMessage.AppendLine().AppendFormat("PayQrcodeScannedPay(false):\n{0}", ex.ToString());
//e.printStackTrace();
//string err = "异常信息:\n" + ex.ToString();
}
return strMessage.ToString();
}

/// <summary>
/// xml数据转DATASET
/// </summary>
/// <param name="xmlData"></param>
/// <returns></returns>
public DataTable ConvertXMLToDt(string xmlData)
{
StringReader stream = null;
XmlTextReader reader = null;

DataSet xmlDS = new DataSet();
stream = new StringReader(xmlData);
//从stream装载到XmlTextReader
reader = new XmlTextReader(stream);
xmlDS.ReadXml(reader);
reader.Close();
return xmlDS.Tables[0];
}
}

#region 微信扫码出参实体
public class Goods_detailItem
{
/// <summary>
///
/// </summary>
public string goods_id { get; set; }
/// <summary>
///
/// </summary>
public int quantity { get; set; }
/// <summary>
///
/// </summary>
public int price { get; set; }
/// <summary>
///
/// </summary>
public int discount_amount { get; set; }
/// <summary>
///
/// </summary>
public string goods_remark { get; set; }
}

public class Promotion_detailItem
{
/// <summary>
///
/// </summary>
public string promotion_id { get; set; }
/// <summary>
/// 单品券
/// </summary>
public string name { get; set; }
/// <summary>
///
/// </summary>
public string scope { get; set; }
/// <summary>
///
/// </summary>
public string type { get; set; }
/// <summary>
///
/// </summary>
public int amount { get; set; }
/// <summary>
///
/// </summary>
public string activity_id { get; set; }
/// <summary>
///
/// </summary>
public int wxpay_contribute { get; set; }
/// <summary>
///
/// </summary>
public int merchant_contribute { get; set; }
/// <summary>
///
/// </summary>
public int other_contribute { get; set; }
/// <summary>
///
/// </summary>
public List<Goods_detailItem> goods_detail { get; set; }
}

public class Response_biz_content
{
/// <summary>
///
/// </summary>
public string return_code { get; set; }
/// <summary>
///
/// </summary>
public string return_msg { get; set; }
/// <summary>
///
/// </summary>
public string pay_status { get; set; }
/// <summary>
///
/// </summary>
public string cust_id { get; set; }
/// <summary>
///
/// </summary>
public string card_no { get; set; }
/// <summary>
///
/// </summary>
public string total_amt { get; set; }
/// <summary>
///
/// </summary>
public string out_trade_no { get; set; }
/// <summary>
///
/// </summary>
public string order_id { get; set; }
/// <summary>
///
/// </summary>
public string pay_time { get; set; }
/// <summary>
/// 工商银行
/// </summary>
public string bank_name { get; set; }
/// <summary>
///
/// </summary>
public string channel { get; set; }
/// <summary>
///
/// </summary>
public string tp_cust_id { get; set; }
/// <summary>
///
/// </summary>
public string trx_ser_no { get; set; }
/// <summary>
///
/// </summary>
public string tp_order_id { get; set; }
/// <summary>
///
/// </summary>
public string buyer_logon_id { get; set; }
/// <summary>
///
/// </summary>
public string sub_open_id { get; set; }
/// <summary>
///
/// </summary>
public string bank_type { get; set; }
/// <summary>
///
/// </summary>
public List<Promotion_detailItem> promotion_detail { get; set; }
/// <summary>
///
/// </summary>
public string tp_user_id { get; set; }
}
#endregion
}



posted @ 2023-02-24 10:41  AngelBoys  阅读(3081)  评论(0编辑  收藏  举报