我有一个像这样的 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 对象的一般文档,但尚未单击任何内容。
1 回答
慕田峪9158850
TA贡献1794条经验 获得超7个赞
为了将 json 变成所需的形状,我这样做了:
var result = "{\"data\":" + jsonString + "}";
其中 jsonString 等于该线程顶部显示的对象数组,因此我只是手动添加这些值。
我还必须改变这一点:
request.Content = new StringContent(JsonConvert.SerializeObject(result)) request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
到:
request.Content = new StringContent(asjson.ToString(), Encoding.UTF8, "application/json");
- 1 回答
- 0 关注
- 98 浏览
添加回答
举报
0/150
提交
取消