如何将Json字符串转换为c#中的键值对?

时间:2022-10-26 16:23:16
   {
      "transactionId" : XXXXX,
      "uri" : "https://XXX.XXXXXXXX.XXXX/XXX/XXX",
      "terminalId" : 1,
      "action" : "CHARGE",
      "amountBase" : "3.00",
      "amountTotal" : "3.00",
      "status" : "CAPTURE",
      "created" : "2015-01-24T07:24:10Z",
      "lastModified" : "2015-01-24T07:24:10Z",
      "response" : 
       {
           "approved" : true,
           "code" : "00",
           "message" : "Approved",
           "processor" : 
            {
               "authorized" : true,
               "approvalCode" : "XXXX",
               "avs" : 
                     {
                         "status" : "NOT_REQUESTED"
                     },
            }
  },
  "settlement" : 
   {
        "settled" : false
   },
  "vault" : 
   {
        "type" : "CARD",
        "accountType" : "VISA",
        "lastFour" : "1111"
  }

}

2 个解决方案

#1


1  

I see this has got a lot of down votes but no one is actually offering any advice. The json above is not a natural fit for a Dictionary but should be de-serialized into an object.

我看到这已经有很多下来的选票,但实际上没有人提供任何建议。上面的json不是自然适合Dictionary,但应该反序列化为一个对象。

Response, settlement and vault all have their own properties and as such should be their own objects.

响应,结算和保险库都有自己的属性,因此应该是他们自己的对象。

Look into Json.net for a good way to convert json into your c# objects. If you are stuck on how you would represent this object in C# then you need to read a good book on programming specifically one that covers Object Orientated programming.

查看Json.net,了解将json转换为c#对象的好方法。如果您不知道如何在C#中表示此对象,那么您需要阅读一本关于编程的好书,其中包括面向对象编程。

Stack is a great resource for these questions but you need to try and show you have done your own research first otherwise others will just mark your questions down.

Stack是这些问题的一个很好的资源,但你需要先尝试表明你已经完成了自己的研究,否则其他人只会将你的问题标记下来。

#2


1  

Are you receiving a POST that contains a JSON ?

您是否收到包含JSON的POST?

You can use something like this , create an instance of Request class and assign the JSON obj to that instance. You should be able to access the parameters via request instance.

您可以使用类似的东西,创建Request类的实例并将JSON obj分配给该实例。您应该能够通过请求实例访问参数。

The basic structure will look some thing like this :

基本结构看起来像这样:

public class Request
{
    public Int64 transactionId { get; set; }
    public string uri { get; set; }
    public int terminalId { get; set; }
    public string action { get; set; }
    public string amountBase { get; set; }
    public int amountTotal { get; set; }
    public string status { get; set; }
    public DateTime created { get; set; }
    public DateTime lastModified { get; set; }
    public Response response { get; set; }
    public Settlement settlement { get; set; }
    public Vault vault { get; set; }
}
public class Response
{
    public bool approved { get; set; }
    public int code { get; set; }
    public string message { get; set; }
    public Processor processor { get; set; }
}
public class Processor
{
    public bool authorized { get; set; }
    public string approvedCode { get; set; }
    public AVS avs { get; set; }
}
public class AVS
{
    public string status { get; set; }
}
public class Settlement
{
    public bool settled { get; set; }
}
public class Vault
{
    public string type { get; set; }
    public string accountType { get; set; }
    public string lastFour { get; set; }
}

Hope this helps !!

希望这可以帮助 !!

#1


1  

I see this has got a lot of down votes but no one is actually offering any advice. The json above is not a natural fit for a Dictionary but should be de-serialized into an object.

我看到这已经有很多下来的选票,但实际上没有人提供任何建议。上面的json不是自然适合Dictionary,但应该反序列化为一个对象。

Response, settlement and vault all have their own properties and as such should be their own objects.

响应,结算和保险库都有自己的属性,因此应该是他们自己的对象。

Look into Json.net for a good way to convert json into your c# objects. If you are stuck on how you would represent this object in C# then you need to read a good book on programming specifically one that covers Object Orientated programming.

查看Json.net,了解将json转换为c#对象的好方法。如果您不知道如何在C#中表示此对象,那么您需要阅读一本关于编程的好书,其中包括面向对象编程。

Stack is a great resource for these questions but you need to try and show you have done your own research first otherwise others will just mark your questions down.

Stack是这些问题的一个很好的资源,但你需要先尝试表明你已经完成了自己的研究,否则其他人只会将你的问题标记下来。

#2


1  

Are you receiving a POST that contains a JSON ?

您是否收到包含JSON的POST?

You can use something like this , create an instance of Request class and assign the JSON obj to that instance. You should be able to access the parameters via request instance.

您可以使用类似的东西,创建Request类的实例并将JSON obj分配给该实例。您应该能够通过请求实例访问参数。

The basic structure will look some thing like this :

基本结构看起来像这样:

public class Request
{
    public Int64 transactionId { get; set; }
    public string uri { get; set; }
    public int terminalId { get; set; }
    public string action { get; set; }
    public string amountBase { get; set; }
    public int amountTotal { get; set; }
    public string status { get; set; }
    public DateTime created { get; set; }
    public DateTime lastModified { get; set; }
    public Response response { get; set; }
    public Settlement settlement { get; set; }
    public Vault vault { get; set; }
}
public class Response
{
    public bool approved { get; set; }
    public int code { get; set; }
    public string message { get; set; }
    public Processor processor { get; set; }
}
public class Processor
{
    public bool authorized { get; set; }
    public string approvedCode { get; set; }
    public AVS avs { get; set; }
}
public class AVS
{
    public string status { get; set; }
}
public class Settlement
{
    public bool settled { get; set; }
}
public class Vault
{
    public string type { get; set; }
    public string accountType { get; set; }
    public string lastFour { get; set; }
}

Hope this helps !!

希望这可以帮助 !!