Mohammad Hossein
Mohammad Hossein

Reputation: 53

Converting list to json in C#

I use this method to generate JSON from the list:

last code

but return JSON has errors like:

expecting object or array, not string
Multiple JSON root elements

you see JSON file in this link and test it in this site:

edit:

I change my code with this:

[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
                                }
                            }
                        };

                        resultList.Add(new JavaScriptSerializer().Serialize(obj));
                    }

                    return resultList;
                }                   
            }

            return message.ProgramError();
        }
        catch (Exception)
        {
            return message.ProgramError();
        }
    }

    private class AllCat
    {
        public int id;
        public string name;
        public string image;
        public SubLevelOne subLevelOne;
    }
    private class SubLevelOne
    {
        public int id;
        public string name;
        public string image;
        public SubLevelTwo subLevelTwo;
    }
    private class SubLevelTwo
    {
        public int id;
        public string name;
        public string image;
    }

Now my json is like this link:

[
   "{\"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\"}}}"
]

but I want sth like this

also waht is ( \ ) is json file?

Upvotes: 0

Views: 8628

Answers (3)

yousef Abbdolzadeh
yousef Abbdolzadeh

Reputation: 420

I think your method type should be JsonResult Like this

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

And In return method you should return Json like this

return Json(model, JsonRequestBehavior.AllowGet);

Upvotes: 0

Diogo Neves
Diogo Neves

Reputation: 644

I'd advise you to use the C# NewtonSoft Json Package available on Nuget Package.

You can just do:

 var resultList = new List<SearchGoods>();

And:

resultList.Add(obj);

In the end just return:

return JsonConvert.SerializeObject(resultList);

And it should give you the correct result.

Upvotes: 4

Farrukh Ahmed
Farrukh Ahmed

Reputation: 493

Your json is in invalid format that's why you are getting all these errors. You have "" surrounded each object and array of your json. Just remove that and you are good to go.

I have cleaned your 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"
          }
        ]
      }
    ]
  }
]

Hope this will help you!

Upvotes: -1

Related Questions