0

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?

10
  • 3
    Why not use Newtonsoft JSON.NET? Commented Aug 15, 2018 at 6:45
  • 5
    This might solve your issue Newtonsoft.Json.JsonConvert.SerializeObject(getCat); Commented Aug 15, 2018 at 6:45
  • 1
    You can use Newtonsoft. Its a third party tool to manage json object with .NET projects. Download Newtonsoft.Json Nuget and refer tutorial. Its really very easy. Commented Aug 15, 2018 at 6:47
  • 1
    @MohitShrivastava why you are constructing JSON like you tried ex. in the question? Commented Aug 15, 2018 at 6:47
  • 2
    Don't create the json string yourself. Build your objects in a way, they represent the final desired JSON structure and then use JsonConvert.SerializeObject() on the topmost element, like you already do for your resultList. BTW According to JSON specification property names and string values must be enclosed by doublequotes " and not by single quotes ' like you do. Commented Aug 15, 2018 at 6:51

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you. I resolve problem with this answer. But now I want to make subLevelOne and subLevelTwo like array with ( [ ] ) but my json is like this: uupload.ir/files/hbxk_bc71612c-03b8-4da6-814d-cb0626af496a.jpg
0

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);

Comments

-1

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.