我想像这样制作一个json字符串并在ASP.NET C#中发布 [英] I want make a json string like this and post in ASP.NET C#

查看:67
本文介绍了我想像这样制作一个json字符串并在ASP.NET C#中发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个像这样的json字符串并在asp.net中发布c#

I want make a json string like this and post in asp.net c#

{
  "data": {
"amount":100,"currencyType":"MYR","expiry":{"type":"PERMANENT"},"isPreFillAmount":true,"method":null,"order":{"detail":"detail","title":"title"},"redirectUrl":"https://google.com","storeId":"1234","type":"DYNAMIC"
  },
  "method": "post",
  "nonceStr": "MNhrdDgIDlTKdlYXzfhEvqHpGjRhvrPe",
  "privateKey": "123456",
  "requestUrl": "https://sb-open.revenuemonster.my/v3/payment/transaction/qrcode",
  "signType": "sha256",
  "timestamp": "1532069238"
}







..在我使用下面之前它工作但现在的问题是如何使数据有像数组里面的json字符串有效期和订单数据在括号中











感谢任何帮助



我尝试过:






.. Before i use like below it worked but now the problem is how to make the json string where data have like array inside have expiry and orders have data in bracket





Appreciate any help

What I have tried:

StreamWriter(httpWebRequest.GetRequestStream()))
{
    string jsonsign1 = new JavaScriptSerializer().Serialize(new
    {


        data = "{" + "amount" + ":" + "100" + "," + "currencyType" + ":" + "MYR" + "}",
        method = "post",
        nonceStr = nonce,
        privateKey = "123",
        requestUrl = "https://sb-open.revenuemonster.my/v3/payment/transaction/qrcode",
        signType = "sha256",
        timestamp = timestampStr

    });

    streamWriter.Write(jsonsign1);
}

推荐答案

string jsonsign1 = new JavaScriptSerializer().Serialize(new
{
    data = new
    {
        amount = 100,
        currencyType = "MYR",
        expiry = new { type = "PERMANENT" },
        isPreFillAmount = true,
        order = new 
        { 
            detail = "detail",
            title = "title"
        },
        redirectUrl = "https://google.com",
        storeId = "1234",
        type = "DYNAMIC"
    },
    method = "post",
    nonceStr = nonce,
    privateKey = "123",
    requestUrl = "https://sb-open.revenuemonster.my/v3/payment/transaction/qrcode",
    signType = "sha256",
    timestamp = timestampStr
});


我建​​议你创建一个具体的对于每个对象,您想要作为输出返回而不是依赖在匿名类型上。这样,您可以轻松修改要添加/删除的对象,并且可以轻松维护它。例如,您可以创建以下类:



I'd suggest you to create a concrete class for each object that you want to return as an output instead of relying on anonymous types. This way you could easily modify the object that you want to add/remove and it's easy to maintain it. For example you could create the following class:

public class Response{
    public Data Data { get; set; }
    public string Method { get; set; }
    public string NonceStr { get; set; }
    public string PrivateKey { get; set; }
    public string RequestUrl { get; set; }
    public string SignType { get; set; }
    public DateTime TimeStamp { get; set; }
}

public class Data{
    public decimal Amount { get; set; }
    public string CurrencyType { get; set; }
    public Expiry Expiry { get; set; }
    public bool IsPreFillAmount { get; set;}
    public Order Order { get; set;}
    public string RedirectUrl { get; set;}
    public string StoreId { get; set;}
    public string Type { get; set;}
}

public class Order{
    public string Detail { get; set;}
    public string Title { get; set;}
}

public class Expiry{
    public string Type { get; set;}
}







然后使用 Newtonsoft.Json 将对象序列化为 JSON 这样的字符串:






You then use Newtonsoft.Json to serialize the object as JSON string like this:

 Response data = new Response(){
				Data = new Data(){
					Amount = 100,
        			CurrencyType = "MYR",
        			Expiry = new Expiry() { Type = "PERMANENT" },
        			IsPreFillAmount = true,
        			Order = new Order() 
        			{ 
            			Detail = "detail",
            			Title = "title"
        			},
        			RedirectUrl = "https://google.com",
        			StoreId = "1234",
        			Type = "DYNAMIC"
				},
				Method = "Post",
				NonceStr = "nonce",
    			PrivateKey = "123",
    			RequestUrl = "https://sb-open.revenuemonster.my/v3/payment/transaction/qrcode",
    			SignType = "sha256",
    			TimeStamp = DateTime.Now
};
			
string jsonResponse = JsonConvert.SerializeObject(data);







变量 jsonResponse 应该返回如下内容:




The variable jsonResponse should return something like this:

{
  "Data": {
    "Amount": 100,
    "CurrencyType": "MYR",
    "Expiry": {
      "Type": "PERMANENT"
    },
    "IsPreFillAmount": true,
    "Order": {
      "Detail": "detail",
      "Title": "title"
    },
    "RedirectUrl": "https://google.com",
    "StoreId": "1234",
    "Type": "DYNAMIC"
  },
  "Method": "Post",
  "NonceStr": "nonce",
  "PrivateKey": "123",
  "RequestUrl": "https://sb-open.revenuemonster.my/v3/payment/transaction/qrcode",
  "SignType": "sha256",
  "TimeStamp": "2018-12-21T02:37:17.5973561+00:00"
}


这篇关于我想像这样制作一个json字符串并在ASP.NET C#中发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆