如何在属性中传递 json 对象数组?

我有一个像这样的 json 对象数组:


[{

    "Time-VitalsTime": "2019-06-22T01:01:00",

    "TimeToEstimate-VitalsTime": 10,

    "TimeToEstimate-1unit": 5,

    "TimeToEstimate-2units": 9,

    "TimeToEstimate-3units": 21,

    "TimeToEstimate-1week": 13,

    "TimeToEstimate-2weeks": 16,

    "TimeToEstimate-3weeks": 20

}, {

    "Time-VitalsTime": "2019-06-22T01:02:00",

    "TimeToEstimate-VitalsTime": 8,

    "TimeToEstimate-1unit": 10,

    "TimeToEstimate-2units": 5,

    "TimeToEstimate-3units": 9,

    "TimeToEstimate-1week": 18,

    "TimeToEstimate-2weeks": 15,

    "TimeToEstimate-3weeks": 32

}, {

    "Time-VitalsTime": "2019-06-22T01:03:00",

    "TimeToEstimate-VitalsTime": 10,

    "TimeToEstimate-1unit": 8,

    "TimeToEstimate-2units": 10,

    "TimeToEstimate-3units": 5,

    "TimeToEstimate-1week": 15,

    "TimeToEstimate-2weeks": 14,

    "TimeToEstimate-3weeks": 16

}, 

我需要把它变成这个形状(根据 api 文档):


{

"data":

    [

        <model-specific-data-structure>

    ]

}

然后我用这个:


 request.Content = new StringContent(JsonConvert.SerializeObject(stringgeneratedabove));

将其序列化以发送到 REST api。我从通话中得到的错误是:“{\”错误\”:\“字符串索引必须是整数\”}”


一位朋友说我需要将数组传递到名为“data”的属性中。在网上进行了大量搜索后,我不知道从哪里开始。我尝试做这样的事情:


[DataContract]

public class CsvDataClass

{

    [DataMember]

    public List<Dictionary<object, object>> CsvData { get; set; }


    [DataMember]

    public Dictionary<object, object>[] Data

    {

        get { return CsvData.ToArray(); }

    }

}

根据朋友的说法,但无法实施。我搜索过“更改 json c# 的形状”或“将 json 数组传递到属性”,并且还阅读了有关 json 对象的一般文档,但尚未单击任何内容。


繁星点点滴滴
浏览 100回答 1
1回答

慕田峪9158850

为了将 json 变成所需的形状,我这样做了:var&nbsp;result&nbsp;=&nbsp;"{\"data\":"&nbsp;+&nbsp;jsonString&nbsp;+&nbsp;"}";其中 jsonString 等于该线程顶部显示的对象数组,因此我只是手动添加这些值。我还必须改变这一点:request.Content&nbsp;=&nbsp;new&nbsp;StringContent(JsonConvert.SerializeObject(result)) request.Content.Headers.ContentType&nbsp;=&nbsp;new&nbsp; MediaTypeHeaderValue("application/json");到:request.Content&nbsp;=&nbsp;new&nbsp;StringContent(asjson.ToString(),&nbsp;Encoding.UTF8,&nbsp;"application/json");
打开App,查看更多内容
随时随地看视频慕课网APP