第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在 C# 中將列表轉(zhuǎn)換為 json

在 C# 中將列表轉(zhuǎn)換為 json

C#
不負(fù)相思意 2021-11-28 19:29:17
我使用此方法從列表中生成 JSON:last code但返回 JSON 有如下錯(cuò)誤:期望對(duì)象或數(shù)組,而不是字符串多個(gè) JSON 根元素您在此鏈接中看到 JSON 文件并在此站點(diǎn)中對(duì)其進(jìn)行測試:編輯:我用這個(gè)更改我的代碼:[HttpPost]    [Route("api/Users/GetAllGoodInCat")]    public object GetAllGoodInCat([FromBody]GoodsCatId goodsCatId)    {        try        {            if (goodsCatId.id != 0)            {                var getCat = (from a in db.goodsGroups                                 where a.Id == goodsCatId.id                                 select a).SingleOrDefault();                if (getCat != null)                {                    var getAllfood = from a in db.goods                        where a.groupId == goodsCatId.id                        orderby a.Id                        select a;                    var resultList = new List<string>();                    foreach (var good in getAllfood)                    {                        var obj = new SearchGoods()                        {                            good = new MyGoods                            {                                id = good.Id,                                name = good.name,                                price = good.price,                                brand = new MyGoodsBrand                                {                                    id = getCat.Id,                                    name = getCat.title,                                    image = getCat.image                                }                            }                        };現(xiàn)在我的 json 就像這個(gè)鏈接:[   "{\"good\":{\"id\":1,\"name\":\"???? ???? ???? ????\",\"price\":20000,\"brand\":{\"id\":22,\"name\":\"???? ????\",\"image\":\"testmy.png\"}}}",   "{\"good\":{\"id\":2,\"name\":\"???? ????\",\"price\":30000,\"brand\":{\"id\":22,\"name\":\"???? ????\",\"image\":\"testmy.png\"}}}",   "{\"good\":{\"id\":3,\"name\":\"???? ?? ????\",\"price\":15000,\"brand\":{\"id\":22,\"name\":\"???? ????\",\"image\":\"testmy.png\"}}}"]但我想要這樣的東西還有 (\) 是 json 文件嗎?
查看完整描述

3 回答

?
狐的傳說

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超3個(gè)贊

我建議您使用Nuget Package 上提供的 C# NewtonSoft Json Package。

你可以這樣做:

 var resultList = new List<SearchGoods>();

和:

resultList.Add(obj);

最后只返回:

return JsonConvert.SerializeObject(resultList);

它應(yīng)該給你正確的結(jié)果。


查看完整回答
反對(duì) 回復(fù) 2021-11-28
?
回首憶惘然

TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超11個(gè)贊

我認(rèn)為你的方法類型應(yīng)該是 JsonResult 像這樣

  public JsonResualt GetAllGoodInCat([FromBody]GoodsCatId goodsCatId){}

在返回方法中,您應(yīng)該像這樣返回 Json

return Json(model, JsonRequestBehavior.AllowGet);


查看完整回答
反對(duì) 回復(fù) 2021-11-28
?
慕尼黑5688855

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊

您的 json 格式無效,這就是您收到所有這些錯(cuò)誤的原因。你有 "" 包圍了你的 json 的每個(gè)對(duì)象和數(shù)組。只需刪除它,你就可以開始了。


我已經(jīng)清理了你的 json :


[


  {

    "id": 2,

    "name": "???????",

    "image": "test.png",

    "subLevelOne": [

      {

        "id": 11,

        "parentId": 2,

        "name": "??????",

        "image": "mytest.png",

        "subLevelTwo": [

          {

            "id": 25,

            "parentId": 11,

            "name": "?????? ????",

            "image": "testmy.png"

          }

        ]

      }

    ]

  },

  {

    "id": 2,

    "name": "???????",

    "image": "test.png",

    "subLevelOne": [

      {

        "id": 12,

        "parentId": 2,

        "name": "??????",

        "image": "mytest.png",

        "subLevelTwo": [

          {

            "id": 26,

            "parentId": 12,

            "name": "?????? ?? ???",

            "image": "testmy.png"

          }

        ]

      }

    ]

  },

  {

    "id": 3,

    "name": "????? ?????",

    "image": "test.png",

    "subLevelOne": [

      {

        "id": 9,

        "parentId": 3,

        "name": "????",

        "image": "mytest.png",

        "subLevelTwo": [

          {

            "id": 22,

            "parentId": 9,

            "name": "???? ????",

            "image": "testmy.png"

          }

        ]

      }

    ]

  },

  {

    "id": 3,

    "name": "????? ?????",

    "image": "test.png",

    "subLevelOne": [

      {

        "id": 10,

        "parentId": 3,

        "name": "????",

        "image": "mytest.png",

        "subLevelTwo": [

          {

            "id": 24,

            "parentId": 10,

            "name": "???? ????",

            "image": "testmy.png"

          }

        ]

      }

    ]

  },

  {

    "id": 4,

    "name": "??????",

    "image": "test.png",

    "subLevelOne": [

      {

        "id": 13,

        "parentId": 4,

        "name": "????",

        "image": "mytest.png",

        "subLevelTwo": [

          {

            "id": 27,

            "parentId": 13,

            "name": "???? ????",

            "image": "testmy.png"

          }

        ]

      }

    ]

  },

  {

    "id": 4,

    "name": "??????",

    "image": "test.png",

    "subLevelOne": [

      {

        "id": 14,

        "parentId": 4,

        "name": "??????",

        "image": "mytest.png",

        "subLevelTwo": [

          {

            "id": 28,

            "parentId": 14,

            "name": "?????? ????",

            "image": "testmy.png"

          }

        ]

      }

    ]

  },

  {

    "id": 5,

    "name": "????? ? ???? ?????",

    "image": "test.png",

    "subLevelOne": [

      {

        "id": 15,

        "parentId": 5,

        "name": "?? ????",

        "image": "mytest.png",

        "subLevelTwo": [

          {

            "id": 29,

            "parentId": 15,

            "name": "?? ????",

            "image": "testmy.png"

          }

        ]

      }

    ]

  },

  {

    "id": 5,

    "name": "????? ? ???? ?????",

    "image": "test.png",

    "subLevelOne": [

      {

        "id": 16,

        "parentId": 5,

        "name": "?????",

        "image": "mytest.png",

        "subLevelTwo": [

          {

            "id": 30,

            "parentId": 16,

            "name": "????? ?????",

            "image": "testmy.png"

          }

        ]

      }

    ]

  },

  {

    "id": 6,

    "name": "????? ? ???????",

    "image": "test.png",

    "subLevelOne": [

      {

        "id": 17,

        "parentId": 6,

        "name": "??????",

        "image": "mytest.png",

        "subLevelTwo": [

          {

            "id": 31,

            "parentId": 17,

            "name": "?????? ????",

            "image": "testmy.png"

          }

        ]

      }

    ]

  },

  {

    "id": 6,

    "name": "????? ? ???????",

    "image": "test.png",

    "subLevelOne": [

      {

        "id": 18,

        "parentId": 6,

        "name": "??????",

        "image": "mytest.png",

        "subLevelTwo": [

          {

            "id": 32,

            "parentId": 18,

            "name": "?????? ??????",

            "image": "testmy.png"

          }

        ]

      }

    ]

  },

  {

    "id": 7,

    "name": "?????? ? ????????",

    "image": "test.png",

    "subLevelOne": [

      {

        "id": 19,

        "parentId": 7,

        "name": "???",

        "image": "mytest.png",

        "subLevelTwo": [

          {

            "id": 33,

            "parentId": 19,

            "name": "??? ????",

            "image": "testmy.png"

          }

        ]

      }

    ]

  },

  {

    "id": 7,

    "name": "?????? ? ????????",

    "image": "test.png",

    "subLevelOne": [

      {

        "id": 20,

        "parentId": 7,

        "name": "????",

        "image": "mytest.png",

        "subLevelTwo": [

          {

            "id": 34,

            "parentId": 20,

            "name": "???? ????",

            "image": "testmy.png"

          }

        ]

      }

    ]

  }

]

希望能幫到你!


查看完整回答
反對(duì) 回復(fù) 2021-11-28
  • 3 回答
  • 0 關(guān)注
  • 403 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)