locked
Can't process JSON data RRS feed

  • Question

  • User565442435 posted

    I'm trying to parse a simple JSON string
    [{"url":"http://loyalty.bosselman.com/Home/tabid/57/Default.aspx","position":"0","roles":"All Users;Administrators;"}]

    You can find it here

    http://loyalty.bosselman.com/DesktopModules/MyServices/API/Welcome/GetMenu?key

    When I run this code

                var baseUrl = "http://loyalty.bosselman.com/DesktopModules/MyServices/API/Welcome/GetMenu?key";
    
                WebClient client = new WebClient();
                var data = client.DownloadString(baseUrl);
    
    
                var obj = JsonConvert.DeserializeObject(data);
    
                JObject job = JObject.Parse(obj.ToString());
    
                foreach (JProperty prop in job.Properties())
                {
                    Response.Write(prop["url"]);
                }

    I get this error

    Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.

    If I inspect my obj element it looks like this

    "[{\"url\":\"http://loyalty.bosselman.com/Home/tabid/57/Default.aspx\",\"position\":\"0\",\"roles\":\"All Users;Administrators;\"}]"

    I'm not sure why the \" are getting in there and obj.replace("\\", ""); does not remove them. What am I doing wrong?

    Wednesday, November 13, 2013 5:20 PM

Answers

  • User565442435 posted

    I tried every form of jobject, jarray, deserialize I could think of. Ended up just changing my web service to deliver xml because I needed to get it done.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 19, 2013 9:27 AM

All replies

  • User-488622176 posted

    what if you remove the "[" and "]" ?

    Thursday, November 14, 2013 3:49 AM
  • User565442435 posted

    I tried that, I think the prolem is for some reason, it's escaping the ". i.e. "url":"http... becomes \"url\":\"http...

    I'm just using the newton json stuff. To create the Json string I have an object

            /// <summary>
            /// this is the class for the items to set in a menu
            /// </summary>
            public class MenuHolder{
    
                [JsonProperty]
                public string url {get; set;}
    
                [JsonProperty]
                public int position { get; set; }
    
                [JsonProperty]
                public string roles {get; set;}
    
            }

    Then a list of it that I add the class MenuHolder to multiple times

    List<MyFunctions.MenuHolder> jsobObjs = new List<MyFunctions.MenuHolder>();

    So something like

    MyFunctions.MenuHolder obj1 = new MyFunctions.MenuHolder();
                MyFunctions.MenuHolder obj2 = new MyFunctions.MenuHolder();
                MyFunctions.MenuHolder obj3 = new MyFunctions.MenuHolder();
    
                obj1.url = "http://somedomain.com";
                obj1.position = 1;
                obj1.roles = "Administrator";
    
                obj2.url = "http://somedomain.com";
                obj2.position = 2;
                obj2.roles = "Administrator";
    
                obj3.url = "http://somedomain.com";
                obj3.position = 3;
                obj3.roles = "Administrator";
    
                jsobObjs.Add(obj1);
                jsobObjs.Add(obj2);
                jsobObjs.Add(obj3);

    Then I return

    JsonConvert.SerializeObject(jsobObjs);

    My code is more complicated on how it gets the values for the objects, but I simplified it for here. Like I said, if you browse to that url, you can see the json string and it looks correct, but when I try to parse it, I get escaped stuff.


    Thursday, November 14, 2013 9:48 AM
  • User-488622176 posted

    Alternative : try to convert/pars JArray (as there is a [ and ] in the result...

    Tuesday, November 19, 2013 9:26 AM
  • User565442435 posted

    I tried every form of jobject, jarray, deserialize I could think of. Ended up just changing my web service to deliver xml because I needed to get it done.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 19, 2013 9:27 AM