Action 参数接收整理, 用 dynamic 类型接收json数据

Action 参数接收整理, 用 dynamic 类型接收json数据

json 转 dynamic

dynamic dynamicObj = JsonConvert.DeserializeObject<dynamic>(jsondata); int code = dynamicObj.code

model 转json

string json = JsonConvert.SerializeObject(new                 a = a,             

1.Action接收json对象

public ActionResult AddGoodsForList(List_Model listModel, List<GoodsModel> goodsModelList, int gs_price_use = 0){         1. Content-Type: application/json; charset=utf-8 // charset=utf-8可省,           {'gs_price_use':'0','goodsModelList':[{'number':'1','gs_no':'G00000002','gs_name':'111','duty_type':'增值税发票','gs_price':0},{'number':'1','gs_no':'G00000002','gs_name':'111','duty_type':'增值税发票','gs_price':0}],'listModel':{'ls_no':'1202','st_no':'01','list_no':'XS01201906000042'},'comid':'52'}         */

 }

2.Action接收非json参数 //a=1&b=2   //Content-Type:text/html; charset=utf-8

  2.1 可直接用含有参数属性的TestModel 直接接收,实例名任意

public ActionResult Demo(TestModel model) } public ActionResult Demo(int a,string b) }

  2.3 接受列表方法:

public ActionResult SubmitCartItem(CartModel model)

 

3.用 dynamic 类型接收json数据

public class XunKuModel         public int state = -1;         public dynamic data ="";
    }

string rst = "{\"data\":{\"UserID\":5615,\"Status\":1,\"Message\":\"登录成功\",\"Token\":\"336c2ce2-f810-4ba6-9049-31f5a78ae5d0\"},\"message\":\"\",\"state\":0}";

XunKuModel model = JLLib.JsonHelper.GetObjByJson<XunKuModel>(rst);

if(model.state==0)                     var lmodel = model.data;                     {

                        //if (IsPropertyExist(lmodel,"Token"))//判断存在                         string UserID = lmodel["UserID"].ToString();                 }

 

4.微信利用小程序js_code获取openid

string url = "https://api.weixin.qq.com/sns/jscode2session?appid="+AppConst.AppId+"&amp;secret="+AppConst.AppSecret+"&amp;js_code="+js_code+"&amp;grant_type=authorization_code";             dynamic d = JsonHelper.GetObjByJson<dynamic>(data);             string expires_in = "";             if (IsPropertyExist(d,"session_key"))             if (IsPropertyExist(d,"expires_in"))             if (IsPropertyExist(d,"openid"))
                openid = d["openid"];

 

5.谷歌浏览器Console 测试脚本 ajaxPostJson为自己封装执行ajax

var data ={'gs_price_use':'0','goodsModelList':[{'number':'1','gs_no':'G00000002','gs_name':'111','duty_type':'增值税发票','gs_price':0},{'number':'1','gs_no':'G00000002','gs_name':'111','duty_type':'增值税发票','gs_price':0}],'listModel':{'ls_no':'1202','st_no':'01','list_no':'XS01201906000042'},'comid':'52'};ajaxPostJson("/AppApi/AddGoodsForList",JSON.stringify(data),function(res){console.log(JSON.stringify(res));});

 

function ajaxPostJson(url, dataMap, fnSuccess) {         $.ajax({             url: url,             contentType: 'application/json',             complete: function () { zzc.hide(); },                 var res = {};                 if (err != null) {                     res.content = decodeURI(err);                 }             },                 zzc.hide();             }     } catch (e) {         showMsg(e.message); }

function ajaxPost(url, dataMap, fnSuccess) {         $.ajax({             url: url,             beforeSend: function () { zzc.show(); },             success: function (data, textStatus, jqXHR) {                 var err = jqXHR.getResponseHeader("JLBerror");                     res.state = "error";                     data = JSON.stringify(res);                 fnSuccess(data);             error: function (XMLHttpRequest, textStatus, errorThrown) {                 showMsg("出错了!状态码:" + XMLHttpRequest.status);         });         zzc.hide();     }
}

 

//android post json: | MediaType JSON=MediaType.parse("application/json; charset=utf-8"); RequestBody requestBody = RequestBody.create(JSON, json); Request request = new Request.Builder() .url("http://192.168.0.102:8080/TestProject/JsonServlet") .post(requestBody) .build(); Response response=okHttpClient.newCall(request).execute();

RequestBody requestBody = RequestBody.create(JSON, json);

.url("http://192.168.0.102:8080/TestProject/JsonServlet")

.build();

posted @ 2021-01-05 13:25  不要摸我的腰  阅读(312)  评论(0编辑  收藏  举报