如何使用 json.net 修复无效的 JSON 并在列表中反序列化<>



当我尝试部署json时,出现错误:

Newtonsoft.Json.JsonReaderException: Additional text encountered after finished reading JSON content: [. Path '', line 1, position 37.

当我在谷歌做很多搜索,我得到的json是无效的,因为它有multiple rootsjson是:

{"slotId":"1","slotName":"9:00 am"}{"slotId":"3","slotName":"9:15 am"}{"slotId":"4","slotName":"9:15 am"}{"slotId":"6","slotName":"9:30 am"}{"slotId":"7","slotName":"9:30 am"}{"slotId":"8","slotName":"9:45 am"}{"slotId":"9","slotName":"9:45 am"}{"slotId":"10","slotName":"10:00 am"}{"slotId":"11","slotName":"10:15 am"}{"slotId":"12","slotName":"10:30 am"}

我试图在列表中反序列化<>:

                mSlotType = JsonConvert.DeserializeObject<List<SlotType>>(json);
            mAdapter = new SlotTimeAdapter(this, Resource.Layout.MrngTimeSlot_ListView_Rowlayout, mSlotType);

json类是:

 class SlotType
{
    public string slotId { get; set; }
    public string slotName { get; set; } 
    public class RootObject
    {
        public List<SlotType> slottype { get; set; }
    }
}

请帮帮我

开发json的php代码为:

$Details = array();
    $detail = array("slotId" => $row['slotId'],
                        "slotName" => $row['slotName'],
                            );
        array_push($Details, $detail);
    echo json_encode($Details );  

当我尝试检查https://jsonformatter.curiousconcept.com/上的json时,它给了我一个输出:

`[{
  "slotId":"1",
  "slotName":"9:00 am"}][{
  "slotId":"3",
  "slotName":"9:15 am"}][{
  "slotId":"4",
  "slotName":"9:15 am"}][{
  "slotId":"6",
  "slotName":"9:30 am"}][{
  "slotId":"7",
  "slotName":"9:30 am"}][{
  "slotId":"8",
  "slotName":"9:45 am"}][{
  "slotId":"9",
  "slotName":"9:45 am"}][{
  "slotId":"10",
  "slotName":"10:00 am"}][{
  "slotId":"11",
  "slotName":"10:15 am"}][{
  "slotId":"12",
  "slotName":"10:30 am"}]`

请帮助我,因为我不明白问题在哪里

这里的问题是你的JSON格式是错误的,它应该看起来像这样:

{
    slottype: [{"slotId":"1","slotName":"9:00 am"},
    {"slotId":"3","slotName":"9:15 am"},
    {"slotId":"4","slotName":"9:15 am"},
    {"slotId":"6","slotName":"9:30 am"},
    {"slotId":"7","slotName":"9:30 am"},
    {"slotId":"8","slotName":"9:45 am"},
    {"slotId":"9","slotName":"9:45 am"},
    {"slotId":"10","slotName":"10:00 am"},
    {"slotId":"11","slotName":"10:15 am"},
    {"slotId":"12","slotName":"10:30 am"}]
}
  • 方括号表示列表类型
  • 列表元素之间需要逗号
  • 你需要根元素结构来更好地匹配你的根对象

相关内容

  • 没有找到相关文章

最新更新