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?

Mohammad Hossein
  • 43
  • 1
  • 2
  • 9
  • 3
    Why not use Newtonsoft JSON.NET? – Susen Maharjan Aug 15 '18 at 06:45
  • 5
    This might solve your issue `Newtonsoft.Json.JsonConvert.SerializeObject(getCat);` – Mohit S Aug 15 '18 at 06:45
  • 1
    You can use [Newtonsoft](https://www.newtonsoft.com/). Its a third party tool to manage json object with .NET projects. Download [Newtonsoft.Json Nuget](https://www.nuget.org/packages/Newtonsoft.Json/) and refer tutorial. Its really very easy. – Gaurang Dave Aug 15 '18 at 06:47
  • 1
    @MohitShrivastava why you are constructing JSON like you tried ex. in the question? – Prashant Pimpale Aug 15 '18 at 06: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. – derpirscher Aug 15 '18 at 06:51
  • @PrashantPimpale: I didnt get you. What are you trying to say with ex. ?? And FYI this is just a comment not answer. – Mohit S Aug 15 '18 at 06:53
  • You can use the same method you are using JsonConvert.SerializeObject to do all this. Perhaps if you could post your class structure with the relationships, it would be easier to help you. – Shahzad Aug 15 '18 at 06:54
  • @MohitShrivastava Sorry, it was a typo mistake. Expected was `MohammadHossein` – Prashant Pimpale Aug 15 '18 at 06:55
  • 1
    Possible duplicate of [How do I turn a C# object into a JSON string in .NET?](https://stackoverflow.com/questions/6201529/how-do-i-turn-a-c-sharp-object-into-a-json-string-in-net) – Abdul Rauf Aug 15 '18 at 07:05
  • I use this link https://stackoverflow.com/questions/6201529/how-do-i-turn-a-c-sharp-object-into-a-json-string-in-net . but now I have other problem. I edited my post to show this problem – Mohammad Hossein Aug 16 '18 at 07:29

3 Answers3

3

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.

Diogo Neves
  • 556
  • 4
  • 14
  • 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: http://uupload.ir/files/hbxk_bc71612c-03b8-4da6-814d-cb0626af496a.jpg – Mohammad Hossein Aug 18 '18 at 06:54
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);
-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!

Farrukh Ahmed
  • 473
  • 4
  • 11
  • 26