2016-07-05 123 views
0

我想创建一个类此这种类型的JSON数据的类如何创建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" 
    } 
    } 
+0

你真的需要一个类或者你想用JSON的设置工作?是http://www.newtonsoft.com/json不是一个解决方案? – tire0011

+0

我真的需要这个类的问题,这个C7D1DDA4-897B-4947-9BC3-074B5D416878我不能创建一个这样的类,我想动态访问它 –

+0

它是在你的手中改变JSON格式?在那里的GUID名称说明和iniside添加属性GUID? – tire0011

回答

2

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

+0

为此我需要创建一个类也是这样的C7D1DDA4-897B-4947-9BC3-074B5D416878,430A7D54-F67C-444E-A888-6898E6D454A8但我不想像 –

+0

那样做你的JSON看起来无效。粘贴完整的JSON那里,为您的网站 – Apoorv

+0

创建一个类我follwed网站通过你的建议,并导致错误的format.The下面的代码生成 公共类项目 { 公共字符串名称{;组; } public string Type {get;组; } public string Value {get;组; } } public class Field { public string id {get;组; } public Item item {get;组; } } public class RootObject { public List Fields {get;组; } } –

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 
} 
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" 
     } 
    } 
    ] 
}