Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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.net在字典中定义带数组的类_C#_Arrays_Json_Dictionary - Fatal编程技术网

C# 使用JSON.net在字典中定义带数组的类

C# 使用JSON.net在字典中定义带数组的类,c#,arrays,json,dictionary,C#,Arrays,Json,Dictionary,我试图在字典中执行以下操作 我可以添加名称和devName很好,但我不确定如何将数组的ReturnedData部分添加到列表中,以使其返回上面的布局 我正在使用的示例代码: febRecords.RootObject febRecordsData = JsonConvert.DeserializeObject<febRecords.RootObject>(serverResponse); Dictionary<string,string> febFormData = ne

我试图在
字典中执行以下操作

我可以添加名称devName很好,但我不确定如何将数组的ReturnedData部分添加到列表中,以使其返回上面的布局

我正在使用的示例代码:

febRecords.RootObject febRecordsData = JsonConvert.DeserializeObject<febRecords.RootObject>(serverResponse);
Dictionary<string,string> febFormData = new Dictionary<string,string>();

febFormData.Add("name", data.firstname.ToString());
febFormData.Add("devName", febData["Data"]["DevisionName"].ToString());
febFormData.Add("ReturnedData", ???); //<-this is where I am stuck

return Ok(JsonConvert.SerializeObject(febFormData, Newtonsoft.Json.Formatting.Indented));
febRecords.RootObject febRecordsData=JsonConvert.DeserializeObject(serverResponse);
Dictionary febFormData=新字典();
febFormData.Add(“name”,data.firstname.ToString());
febFormData.Add(“devName”,febData[“Data”][“DevisionName”].ToString());

febFormData.Add(“ReturnedData”,?)// 正如@Panagiotis Kanavos所建议的,最好使用.NET实体来匹配JSON数据。例如:

// propertyname attributes can be ignored if property names 
// match the json data property names 1:1

[JsonObject]
public class MyClass
{
    public MyClass()
    {
        ReturnedData = new List<ReturnedData>();
    }

    [JsonProperty(PropertyName = "name")]
    public string Name { get; set; }

    [JsonProperty(PropertyName = "devName")]
    public string DevName { get; set; }

    [JsonProperty(PropertyName = "ReturnedData")]
    public List<ReturnedData> ReturnedData { get; set; }
}

[JsonObject]
public class ReturnedData
{
    [JsonProperty(PropertyName = "level_heading")]
    public string LevelHeading { get; set; }

    [JsonProperty(PropertyName = "DeliverBestMedicalValue")]
    public string DeliverBestMedicalValue { get; set; }

    [JsonProperty(PropertyName = "level_question")]
    public string LevelQuestion { get; set; }
}
//如果属性名为
//将json数据属性名称1:1匹配
[JsonObject]
公共类MyClass
{
公共MyClass()
{
ReturnedData=新列表();
}
[JsonProperty(PropertyName=“name”)]
公共字符串名称{get;set;}
[JsonProperty(PropertyName=“devName”)]
公共字符串DevName{get;set;}
[JsonProperty(PropertyName=“ReturnedData”)]
公共列表返回数据{get;set;}
}
[JsonObject]
公共类返回数据
{
[JsonProperty(PropertyName=“level\u heading”)]
公共字符串LevelHeading{get;set;}
[JsonProperty(PropertyName=“DeliverTestMedicalValue”)]
公共字符串DeliverTestMedicalValue{get;set;}
[JsonProperty(PropertyName=“level_question”)]
公共字符串LevelQuestion{get;set;}
}
这将使JSON的转换更加容易

[TestMethod]
public void TestSome()
{
    string json = @"{
        ""name"": ""Bob Barker"",
        ""devName"": ""InformationServices"",
        ""ReturnedData"": [{
            ""level_heading"": ""blah1"",
            ""DeliverBestMedicalValue"": ""blah2"",
            ""level_question"": ""blah3""
        }]
    }";

    var obj = JsonConvert.DeserializeObject<MyClass>(json);
    Assert.IsTrue(JToken.DeepEquals(JObject.Parse(json), JObject.FromObject(obj)));
}
[TestMethod]
公共void TestSome()
{
字符串json=@”{
“姓名”:“鲍勃·巴克”,
“开发名称”:“信息服务”,
“返回数据”:[{
“级别标题”:“blah1”,
“DeliverTestMedicalValue”:“blah2”,
“级别问题”:“blah3”
}]
}";
var obj=JsonConvert.DeserializeObject(json);
IsTrue(JToken.DeepEquals(JObject.Parse(json)、JObject.FromObject(obj));
}

您始终可以使用JSON数据生成存根,例如


这里还有“live.NET fiddle”

您需要哪些数据?为什么要逐段创建字典,而不是使用具有
name
devName
等属性的类?另一方面,您可以从反序列化示例Json开始,并观察结果对象
ReturnedData
看起来像是一个具有属性的对象数组
level_heading
etcI我不确定,但是,请尝试
new Dictionary()更改为
新字典()然后对于返回的数据使用
字典()。。。例如:
Dictionary retData=newdictionary();添加(“级别标题”、“blah1”)
然后
febFormData.Add(“ReturnedData”,retData)。。。我使用
vb.net
,我想这是到
c#
@DanielA.White的正确转换。我想知道如何将数据放入字典中,以便它的格式与我在OP的第一个JSON示例中看到的格式相同。
// propertyname attributes can be ignored if property names 
// match the json data property names 1:1

[JsonObject]
public class MyClass
{
    public MyClass()
    {
        ReturnedData = new List<ReturnedData>();
    }

    [JsonProperty(PropertyName = "name")]
    public string Name { get; set; }

    [JsonProperty(PropertyName = "devName")]
    public string DevName { get; set; }

    [JsonProperty(PropertyName = "ReturnedData")]
    public List<ReturnedData> ReturnedData { get; set; }
}

[JsonObject]
public class ReturnedData
{
    [JsonProperty(PropertyName = "level_heading")]
    public string LevelHeading { get; set; }

    [JsonProperty(PropertyName = "DeliverBestMedicalValue")]
    public string DeliverBestMedicalValue { get; set; }

    [JsonProperty(PropertyName = "level_question")]
    public string LevelQuestion { get; set; }
}
[TestMethod]
public void TestSome()
{
    string json = @"{
        ""name"": ""Bob Barker"",
        ""devName"": ""InformationServices"",
        ""ReturnedData"": [{
            ""level_heading"": ""blah1"",
            ""DeliverBestMedicalValue"": ""blah2"",
            ""level_question"": ""blah3""
        }]
    }";

    var obj = JsonConvert.DeserializeObject<MyClass>(json);
    Assert.IsTrue(JToken.DeepEquals(JObject.Parse(json), JObject.FromObject(obj)));
}