Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 需要帮助序列化/反序列化此JSON吗_C#_Json_Json.net - Fatal编程技术网

C# 需要帮助序列化/反序列化此JSON吗

C# 需要帮助序列化/反序列化此JSON吗,c#,json,json.net,C#,Json,Json.net,我有一个简单的JSON,我正在对这个JSON进行反序列化,这样我就可以得到元素。这是我的JSON: { "QuestionIDs": [ "QID1", "QID3" ], "QuestionDefinitions": { "Question1": { "DETag": "Q1", "Config": { "QDescription": "UseText" }, "COrder": [

我有一个简单的JSON,我正在对这个JSON进行反序列化,这样我就可以得到元素。这是我的JSON:

{
  "QuestionIDs": [
    "QID1",
    "QID3"
  ],
  "QuestionDefinitions": {
    "Question1": {
      "DETag": "Q1",
      "Config": {
        "QDescription": "UseText"
      },
      "COrder": [
        "1",
        "2",
        "3"
      ],
      "Validation": {
        "Settings": {
          "ForceResponse": "OFF",
          "ForceResponseType": "ON",
          "Type": "None"
        }
      }
    }
  },
  "NextButton": null,
  "PreviousButton": false
}
这是我写的代码:

[DataContract]
public class RootObject
{
    [DataMember]
    public Dictionary<string, Dictionary<string, string>> QuestionDefinitions { get; set; }
    [DataMember]
    public List<string> QuestionIDs { get; set; }
}

针对返回的JSON的类结构不正确

将JSON粘贴到以下生成的POCO类中时:

public class Config
{
    public string QDescription { get; set; }
}

public class Settings
{
    public string ForceResponse { get; set; }
    public string ForceResponseType { get; set; }
    public string Type { get; set; }
}

public class Validation
{
    public Settings Settings { get; set; }
}

public class Question1
{
    public string DETag { get; set; }
    public Config Config { get; set; }
    public List<string> COrder { get; set; }
    public Validation Validation { get; set; }
}

public class QuestionDefinitions
{
    public Question1 Question1 { get; set; }
}

public class RootObject
{
    public List<string> QuestionIDs { get; set; }
    public QuestionDefinitions QuestionDefinitions { get; set; }
    public object NextButton { get; set; }
    public bool PreviousButton { get; set; }
}
公共类配置
{
公共字符串QDescription{get;set;}
}
公共类设置
{
公共字符串ForceResponse{get;set;}
公共字符串ForceResponseType{get;set;}
公共字符串类型{get;set;}
}
公共类验证
{
公共设置{get;set;}
}
公开课问题1
{
公共字符串DETag{get;set;}
公共配置{get;set;}
公共列表记录程序{get;set;}
公共验证{get;set;}
}
公共类问题定义
{
公共问题1问题1{get;set;}
}
公共类根对象
{
公共列表问题ID{get;set;}
公共问题定义问题定义{get;set;}
公共对象下一个按钮{get;set;}
公共布尔上一个按钮{get;set;}
}
现在,使用上面的类作为JSON结构的表示,您应该能够将其反序列化为C#对象

以下代码行将使用NewtonSoft库完成此操作:

string json = "your json result";
RootObject result = JsonConvert.DeserializeObject<RootObject>(json);
string json=“您的json结果”;
RootObject result=JsonConvert.DeserializeObject(json);
如果您有2013或更高版本的用户,您甚至可以在Visual Studio中执行此操作,如中所述:


希望有帮助。

针对返回的JSON的类结构不正确

将JSON粘贴到以下生成的POCO类中时:

public class Config
{
    public string QDescription { get; set; }
}

public class Settings
{
    public string ForceResponse { get; set; }
    public string ForceResponseType { get; set; }
    public string Type { get; set; }
}

public class Validation
{
    public Settings Settings { get; set; }
}

public class Question1
{
    public string DETag { get; set; }
    public Config Config { get; set; }
    public List<string> COrder { get; set; }
    public Validation Validation { get; set; }
}

public class QuestionDefinitions
{
    public Question1 Question1 { get; set; }
}

public class RootObject
{
    public List<string> QuestionIDs { get; set; }
    public QuestionDefinitions QuestionDefinitions { get; set; }
    public object NextButton { get; set; }
    public bool PreviousButton { get; set; }
}
公共类配置
{
公共字符串QDescription{get;set;}
}
公共类设置
{
公共字符串ForceResponse{get;set;}
公共字符串ForceResponseType{get;set;}
公共字符串类型{get;set;}
}
公共类验证
{
公共设置{get;set;}
}
公开课问题1
{
公共字符串DETag{get;set;}
公共配置{get;set;}
公共列表记录程序{get;set;}
公共验证{get;set;}
}
公共类问题定义
{
公共问题1问题1{get;set;}
}
公共类根对象
{
公共列表问题ID{get;set;}
公共问题定义问题定义{get;set;}
公共对象下一个按钮{get;set;}
公共布尔上一个按钮{get;set;}
}
现在,使用上面的类作为JSON结构的表示,您应该能够将其反序列化为C#对象

以下代码行将使用NewtonSoft库完成此操作:

string json = "your json result";
RootObject result = JsonConvert.DeserializeObject<RootObject>(json);
string json=“您的json结果”;
RootObject result=JsonConvert.DeserializeObject(json);
如果您有2013或更高版本的用户,您甚至可以在Visual Studio中执行此操作,如中所述:


希望能有所帮助。

对中的代码稍加修改,也许您可以使用以下内容:

public class Config
{
    public string QDescription { get; set; }
}

public class Settings
{
    public string ForceResponse { get; set; }
    public string ForceResponseType { get; set; }
    public string Type { get; set; }
}

public class Validation
{
    public Settings Settings { get; set; }
}

public class Question
{
    public string DETag { get; set; }
    public Config Config { get; set; }
    public List<string> COrder { get; set; }
    public Validation Validation { get; set; }
}

public class RootObject
{
    public List<string> QuestionIDs { get; set; }
    public Dictionary<string, Question> QuestionDefinitions { get; set; }
    public object NextButton { get; set; }
    public bool PreviousButton { get; set; }
}
公共类配置
{
公共字符串QDescription{get;set;}
}
公共类设置
{
公共字符串ForceResponse{get;set;}
公共字符串ForceResponseType{get;set;}
公共字符串类型{get;set;}
}
公共类验证
{
公共设置{get;set;}
}
公开课问题
{
公共字符串DETag{get;set;}
公共配置{get;set;}
公共列表记录程序{get;set;}
公共验证{get;set;}
}
公共类根对象
{
公共列表问题ID{get;set;}
公共词典问题定义{get;set;}
公共对象下一个按钮{get;set;}
公共布尔上一个按钮{get;set;}
}

稍微更改一下中的代码,也许您可以使用以下内容:

public class Config
{
    public string QDescription { get; set; }
}

public class Settings
{
    public string ForceResponse { get; set; }
    public string ForceResponseType { get; set; }
    public string Type { get; set; }
}

public class Validation
{
    public Settings Settings { get; set; }
}

public class Question
{
    public string DETag { get; set; }
    public Config Config { get; set; }
    public List<string> COrder { get; set; }
    public Validation Validation { get; set; }
}

public class RootObject
{
    public List<string> QuestionIDs { get; set; }
    public Dictionary<string, Question> QuestionDefinitions { get; set; }
    public object NextButton { get; set; }
    public bool PreviousButton { get; set; }
}
公共类配置
{
公共字符串QDescription{get;set;}
}
公共类设置
{
公共字符串ForceResponse{get;set;}
公共字符串ForceResponseType{get;set;}
公共字符串类型{get;set;}
}
公共类验证
{
公共设置{get;set;}
}
公开课问题
{
公共字符串DETag{get;set;}
公共配置{get;set;}
公共列表记录程序{get;set;}
公共验证{get;set;}
}
公共类根对象
{
公共列表问题ID{get;set;}
公共词典问题定义{get;set;}
公共对象下一个按钮{get;set;}
公共布尔上一个按钮{get;set;}
}
试试这种型号

public class RootObject
{
    public List<string> QuestionIDs { get; set; }
    public QuestionDefinitions QuestionDefinitions { get; set; }
    public object NextButton { get; set; }
    public bool PreviousButton { get; set; }
}
public class QuestionDefinitions
{
    public Question1 Question1 { get; set; }
    public List<string> COrder { get; set; }
    public Validation Validation { get; set; }
}
public class Question1 {
    public string DETag { get; set; }
    public Config Config { get; set; }
}
public class Config {
    public string QDescription { get; set; }
}
public class Validation {
    public Settings Settings { get; set; }
}
public class Settings
{
    public string ForceResponse { get; set; }
    public string ForceResponseType { get; set; }
    public string Type { get; set; }
}
公共类根对象
{
公共列表问题ID{get;set;}
公共问题定义问题定义{get;set;}
公共对象下一个按钮{get;set;}
公共布尔上一个按钮{get;set;}
}
公共类问题定义
{
公共问题1问题1{get;set;}
公共列表记录程序{get;set;}
公共验证{get;set;}
}
公开课问题1{
公共字符串DETag{get;set;}
公共配置{get;set;}
}
公共类配置{
公共字符串QDescription{get;set;}
}
公共类验证{
公共设置{get;set;}
}
公共类设置
{
公共字符串ForceResponse{get;set;}
公共字符串ForceResponseType{get;set;}
公共字符串类型{get;set;}
}
试试这种型号

public class RootObject
{
    public List<string> QuestionIDs { get; set; }
    public QuestionDefinitions QuestionDefinitions { get; set; }
    public object NextButton { get; set; }
    public bool PreviousButton { get; set; }
}
public class QuestionDefinitions
{
    public Question1 Question1 { get; set; }
    public List<string> COrder { get; set; }
    public Validation Validation { get; set; }
}
public class Question1 {
    public string DETag { get; set; }
    public Config Config { get; set; }
}
public class Config {
    public string QDescription { get; set; }
}
public class Validation {
    public Settings Settings { get; set; }
}
public class Settings
{
    public string ForceResponse { get; set; }
    public string ForceResponseType { get; set; }
    public string Type { get; set; }
}
公共类根对象
{
公共列表问题ID{get;set;}
公共问题定义问题定义{get;set;}
公共对象下一个按钮{get;set;}
公共布尔上一个按钮{get;set;}
}
公共类问题定义
{
公共问题1问题1{get;set;}
公共列表记录程序{get;set;}
公共验证{get;set;}
}
公开课问题1{
公共字符串DETag{get;set;}
公共配置{get;set;}
}
公共类配置{
公共街