如何为json数据创建类

时间:2016-07-05 12:19:37

标签: json asp.net-mvc

我想为这种类型的json数据创建一个类

"Fields": {
    "{C7D1DDA4-897B-4947-9BC3-074B5D416878}": {
      "Name": "subject",
      "Type": "Single-Line Text",
      "Value": "Entry/Withdrawal Confirmation"
    },
    "{430A7D54-F67C-444E-A888-6898E6D454A8}": {
      "Name": "body",
      "Type": "Multi-Line Text",
      "Value": "You have entered $tournament with $playerName at $Location"
    }
  }

3 个答案:

答案 0 :(得分:2)

一种非常简单的方法是使用此在线转换器website。只需在那里粘贴JSON,它就会为你生成类。如果JSON无效,该工具将通知

答案 1 :(得分:0)

以上json有键值对。每当这种类型的json将具有时,我们需要使用Dictionary,因为Dictionary具有键值对。以下是您的代码:

public class YourClassName{
   public string Name {get; set;}
   public string Type {get; set;}
   public string Value {get; set;}
}


public returnType YourServiceName(List<Dictionary<Guid, YourClassName>> model){
//your code will be here
}

答案 2 :(得分:0)

这是一个难以使用的JSON(或者甚至无效,但我不是专家),因为JSON通常由键值对组成,但我们看到的所有GUID实际上都是值用作键。

如果您可以将JSON更改为更像这样,那么处理起来会容易得多:

{
  "Fields": [
    {
      "id": "{C7D1DDA4-897B-4947-9BC3-074B5D416878}",
      "item": {
        "Name": "subject",
        "Type": "Single-Line Text",
        "Value": "Entry/Withdrawal Confirmation"
      }
    },
    {
      "id": "{430A7D54-F67C-444E-A888-6898E6D454A8}",
      "item": {
        "Name": "body",
        "Type": "Multi-Line Text",
        "Value": "You have entered $tournament with $playerName at $Location"
      }
    }
  ]
}