none
Help with Dictonary RRS feed

All replies

  • JsonConvert is part of a third-party library. You should post questions related to third party libraries like JSON.NET in their forums.

    As for the dictionary approach, notice that your JSON actually only has a couple of keys. The data you want is an array under result. So you'd have to get the value for result and then process it. But since you're using object as the value it'll come back as JSON that you'd then need to convert again.

    The easier approach might be to let the converter figure out the type. This simplifies your code dramatically. Of course if you need more than a few values then you should go ahead and define a type to deserialize to instead.

    var value = JsonConvert.DeserializeObject(response) as JObject;
    
    //The result is an array so grab the first one
    var token = value["result"]?.ToArray().FirstOrDefault();
    
    var id = (token != null) ? token["_id"] : null;


    Michael Taylor http://www.michaeltaylorp3.net

    • Proposed as answer by Fei HuModerator Sunday, October 29, 2017 5:23 AM
    • Unproposed as answer by old_School Monday, November 6, 2017 7:13 PM
    Wednesday, October 25, 2017 7:26 PM
    Moderator
  • Thanks I'll give that a shot
    Wednesday, October 25, 2017 8:49 PM
  • Wednesday, October 25, 2017 11:40 PM
  • This did not work. It returns null for the token.

    var value = JsonConvert.DeserializeObject(Response) as JObject;
    
                    //The result is an array so grab the first one
                    var token = value["result"]?.ToArray().FirstOrDefault();
    
                    var id = (token != null) ? token["_id"] : null;

    Monday, November 6, 2017 7:14 PM
  • I've decided to try to just put everything into a class and deseralize my json string. However, I'm getting an error now. I think its related to my class or something.

    Json String:

    "{\"result\":[{\"_id\":\"98d0d0b9-9506-40b4-84c4-e005d4bc25ec\",\"_rev\":\"1\",\"ahaMemberGroup\":[\"AONE\",\"AHA\"]}],\"resultCount\":1,\"pagedResultsCookie\":null,\"totalPagedResultsPolicy\":\"NONE\",\"totalPagedResults\":-1,\"remainingPagedResults\":-1}"

    Class:

    public class LDAPMembership
        {
            public string[] result { get; set; }
            public string _id { get; set; }
            public string _rev { get; set; }
            public string[] ahaMemberGroup { get; set; }
            public string[] ahaDrupalPermissions { get; set; }
            public int resultCount { get; set; }
            public string pagedResultsCookie { get; set; }
            public string totalPagedResultsPolicy { get; set; }
            public int totalPagedResults { get; set; }
            public int remainingPagedResults { get; set; }
        }

    Code:

    var result = JsonConvert.DeserializeObject<LDAPMembership>(Response);
    
                    string id = result._id;

    Error:

    Unexpected character encountered while parsing value: {. Path 'result', line 1, position 12."}

    Monday, November 6, 2017 8:03 PM
  • Your other thread has answers. You must serialize the data into the classes the data represents.


    Sam Hobbs
    SimpleSamples.Info

    Monday, November 6, 2017 8:27 PM
  • What do you mean? that's what I did in the code didn't I?

    Edit:

    I have adjusted the code but still not sure what you mean. Also still getting the same error

    Code:

    Response = OpenIDM.GetUserInformation(email, fields);
                    LDAPMembership = JsonConvert.DeserializeObject<LDAPMembership>(Response);
    
                    string id = LDAPMembership._id;

    Exception:

    ex = {"Unexpected character encountered while parsing value: {. Path 'result', line 1, position 12."}

    • Edited by old_School Monday, November 6, 2017 8:37 PM
    Monday, November 6, 2017 8:31 PM
  • Spreading this issue out into separate threads is confusing. I am not sure what is happening.


    Sam Hobbs
    SimpleSamples.Info

    Monday, November 6, 2017 8:36 PM
  • This issue is posted here and on stack overflow. Two separate sites.
    Monday, November 6, 2017 8:48 PM
  • Yes, the JSON data doesn't match your class definition. The JSON data is a single object with a nested array. 

    public class LDAPResults
    {
        public LDAPMembership[] Result { get; set; }
    
        //Ignoring other members
    }
    
    public class LDAPMembership
    {
        public string _id { get; set; }
        public string _rev { get; set; }
    
        public string UserName { get; set; }
        public string Mail { get; set; }
        public string GivenName { get; set; }
    
        //Ignoring remaining members
    }
    
    //Usage
    var results = JsonConvert.DeserializeObject<LDAPResults>(json);


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, November 6, 2017 9:33 PM
    Moderator
  • You should not post in more than one site at the same time and when you do you should link to the other site from each site. That is a courtesy for others so that they don't waste their time.

    But wait, there is more. You have two threads here in the MSDN forums and you and others are posting to each of the two threads here and that is confusing and a waste of time. I really believe there is at least one answer in the other thread; you just need to stick with it.



    Sam Hobbs
    SimpleSamples.Info

    Tuesday, November 7, 2017 3:30 AM