locked
Help Creating an string array of objects with in a Class RRS feed

  • Question

  • I'm trying to make a property to house a list of objects. Here is my API response:

    {
        "result": [
            {
                "_id": "686fbc60-fff0-48e1-86ac-a7a6ae1e49e0",
                "_rev": "6",
                "userName": "testuser4@aha.org",
                "mail": "testuser4@aha.org",
                "givenName": "Test",
                "sn": "user4",
                "accountStatus": "Active",
                "avectraId": "9999999678",
                "AHApw": "/Z5/5OXUBquzAtXesIjN0WjxFp1z05jMXKoynFyVKwa1+RdaUxmrFU6/nCOLedqh",
                "password": {
                    "$crypto": {
                        "type": "x-simple-encryption",
                        "value": {
                            "cipher": "AES/CBC/PKCS5Padding",
                            "data": "mvhrDP4SO8oPRmbTi9jRKA==",
                            "iv": "h8BeQw2nMLPOLGcOCMHZeg==",
                            "key": "openidm-sym-default"
                        }
                    }
                },
                "ahaBirthYear": "1980",
                "city": "Test",
                "ahaGender": "Female",
                "ahaJobTitle": "CNO",
                "ahaLeadScore": "101",
                "stateProvince": "IL",
                "orgId": "0006430024",
                "ahaMemberGroup": [
                    "AHA",
                    "AONE"
                ],
                "ahaMemberType": [
                    "I-B",
                    "FULL"
                ],
                "regMethod": "AMS",
                "activationToken": "44f10fc20882075b57fe14a6908decbd",
                "activationTokenTS": "1508264760793",
                "ahaMemberships": [],
                "access": []
            }
        ],
        "resultCount": 1,
        "pagedResultsCookie": null,
        "totalPagedResultsPolicy": "NONE",
        "totalPagedResults": -1,
        "remainingPagedResults": -1
    }

    I'm trying to make a class that can I can use to build the property values converting from json to the object. Here is my class currently:

     public class _results
        {
            public string[] result { get; set; }
            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; }
            public string sn { get; set; }
            public string accountStatus { get; set; }
            public string avectraId { get; set; }
            public string AHApw { get; set; }
            public string password { get; set; }
            public string ahaBirthYer { get; set; }
            public string city { get; set; }
            public string ahaGender { get; set; }
            public string ahaJobTitle { get; set; }
            public string ahaLeadScore { get; set; }
            public string stateProvince { get; set; }
            public string orgId { get; set; }
            public string[] ahaMemberGroup { get; set; }
            public string[] ahaMemberType { get; set; }
            public string regMethod { get; set; }
            public string activationToken { get; set; }
            public string activationTokenTS { get; set; }
            public string[] ahaMemberships { get; set; }
            public string[] access { get; set; }
            public string resultCount { get; set; }
            public string pagedResultsCookie { get; set; }
            public string totalPagedResultsPolicy { get; set; }
            public string totalPagedResults { get; set; }
            public string remainingPagedResults { get; set; }
        }

    Here is my deserialization line:

    _results _results = Newtonsoft.Json.JsonConvert.DeserializeObject<_results>(Response);

    Error currently getting:

    "Error reading string. Unexpected token: StartObject. Path 'result[0]', line 1, position 12."}

    Wednesday, October 25, 2017 7:12 PM

Answers

  • Hello Old_School,

    For my class structure, The Rootobject class is a root container that has stored all value from json file, others are served as navigation properties for Rootobject. You don't need to deserialize all classes specified. Just deserialize the Rootobject.

    If you want to populate the property value of other class. Try the below code.

     Rootobject rootobject = Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject>(response);
                var iv=rootobject.result[0].password.crypto.value.iv;        
                var data= rootobject.result[0].password.crypto.value.data;
                string type = rootobject.result[0].password.crypto.type;

    My Class Structure(Notice your json file has invalid character so I take some operation)

     public class Rootobject
        {
            public Result[] result { get; set; }
            public int resultCount { get; set; }
            public object pagedResultsCookie { get; set; }
            public string totalPagedResultsPolicy { get; set; }
            public int totalPagedResults { get; set; }
            public int remainingPagedResults { get; set; }
        }
        public class Result
        {
            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; }
            public string sn { get; set; }
            public string accountStatus { get; set; }
            public string avectraId { get; set; }
            public string AHApw { get; set; }
            public Password password { get; set; }
            public string ahaBirthYear { get; set; }
            public string city { get; set; }
            public string ahaGender { get; set; }
            public string ahaJobTitle { get; set; }
            public string ahaLeadScore { get; set; }
            public string stateProvince { get; set; }
            public string orgId { get; set; }
            public string[] ahaMemberGroup { get; set; }
            public string[] ahaMemberType { get; set; }
            public string regMethod { get; set; }
            public string activationToken { get; set; }
            public string activationTokenTS { get; set; }
            public object[] ahaMemberships { get; set; }
            public object[] access { get; set; }
        }
        public class Password
        {
            //Because the Json file with an invalid character for a field name,
            //So I use JSON attributes to specify the name of the property to map to the model.
            [JsonProperty("$crypto")]
            public Crypto crypto { get; set; }
        }  
         public class Crypto
        {
            public string type { get; set; }
            public Value value { get; set; }
        }
        public class Value
        {
            public string cipher { get; set; }
            public string data { get; set; }
            public string iv { get; set; }
            public string key { get; set; }
        }

    If you have any issues with my reply please feel free to contact me.

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Fei Hu Friday, October 27, 2017 2:50 AM
    • Marked as answer by old_School Tuesday, November 7, 2017 9:46 PM
    Friday, October 27, 2017 2:47 AM

All replies

  • It is expecting an array of "result" objects. So something like the following.

    public class Value
    {
        public string cipher { get; set; }
        public string data { get; set; }
        public string iv { get; set; }
        public string key { get; set; }
    }
    
    public class Crypto
    {
        public string type { get; set; }
        public Value value { get; set; }
    }
    
    public class Password
    {
        public Crypto __invalid_name__$crypto { get; set; }
    }
    
    public class Result
    {
        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; }
        public string sn { get; set; }
        public string accountStatus { get; set; }
        public string avectraId { get; set; }
        public string AHApw { get; set; }
        public Password password { get; set; }
        public string ahaBirthYear { get; set; }
        public string city { get; set; }
        public string ahaGender { get; set; }
        public string ahaJobTitle { get; set; }
        public string ahaLeadScore { get; set; }
        public string stateProvince { get; set; }
        public string orgId { get; set; }
        public List<string> ahaMemberGroup { get; set; }
        public List<string> ahaMemberType { get; set; }
        public string regMethod { get; set; }
        public string activationToken { get; set; }
        public string activationTokenTS { get; set; }
        public List<object> ahaMemberships { get; set; }
        public List<object> access { get; set; }
    }
    
    public class RootObject
    {
        public List<Result> result { get; set; }
        public int resultCount { get; set; }
        public object pagedResultsCookie { get; set; }
        public string totalPagedResultsPolicy { get; set; }
        public int totalPagedResults { get; set; }
        public int remainingPagedResults { get; set; }
    }

    For things like this, I search the internet for a tool to help me. Something like json2csharp; it generated the preceding for us.



    Sam Hobbs
    SimpleSamples.Info

    • Marked as answer by old_School Thursday, October 26, 2017 2:25 PM
    • Unmarked as answer by old_School Thursday, October 26, 2017 2:26 PM
    Wednesday, October 25, 2017 10:38 PM
  • Hello old_School,

    >>"Error reading string. Unexpected token: StartObject. Path 'result[0]', line 1, position 12."}

    The error message shows that you don't create a right container. Beside above tools Sam suggested,it also could be done in visual studio.

    1.Copy your json string

    2.click edit -> select pasate special -> pasate json as classes

    The generated result

    public class Rootobject
        {
            public Result[] result { get; set; }
            public int resultCount { get; set; }
            public object pagedResultsCookie { get; set; }
            public string totalPagedResultsPolicy { get; set; }
            public int totalPagedResults { get; set; }
            public int remainingPagedResults { get; set; }
        }
    
        public class Result
        {
            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; }
            public string sn { get; set; }
            public string accountStatus { get; set; }
            public string avectraId { get; set; }
            public string AHApw { get; set; }
            public Password password { get; set; }
            public string ahaBirthYear { get; set; }
            public string city { get; set; }
            public string ahaGender { get; set; }
            public string ahaJobTitle { get; set; }
            public string ahaLeadScore { get; set; }
            public string stateProvince { get; set; }
            public string orgId { get; set; }
            public string[] ahaMemberGroup { get; set; }
            public string[] ahaMemberType { get; set; }
            public string regMethod { get; set; }
            public string activationToken { get; set; }
            public string activationTokenTS { get; set; }
            public object[] ahaMemberships { get; set; }
            public object[] access { get; set; }
        }
    
        public class Password
        {
            public Crypto crypto { get; set; }
        }
    
        public class Crypto
        {
            public string type { get; set; }
            public Value value { get; set; }
        }
    
        public class Value
        {
            public string cipher { get; set; }
            public string data { get; set; }
            public string iv { get; set; }
            public string key { get; set; }
        }
    
    

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 26, 2017 9:01 AM
  • Ahh thanks everyone. I thought I could just put the result inside an array but that makes sense. Thank you very much. So what obect do I use for the deseralize of the json?

    _results _results = Newtonsoft.Json.JsonConvert.DeserializeObject<_results>(Response);

    You have multipal classes listed, so how does it know how to populate each object?

    Thursday, October 26, 2017 2:27 PM
  • it also could be done in visual studio.

    I should have looked there.


    Sam Hobbs
    SimpleSamples.Info

    Thursday, October 26, 2017 5:02 PM
  • You have multipal classes listed, so how does it know how to populate each object?

    JSON is the format JavaScript uses to store objects externally. Objects in JavaScript are relatively simple. JSON is mostly a set of key/value pairs. Square brackets indicate arrays and curly brackets indicate hierarchy. As far as I know that is essentially everything there is to the format. See Introducing JSON for another description.


    Sam Hobbs
    SimpleSamples.Info

    • Proposed as answer by Fei Hu Wednesday, November 1, 2017 8:46 AM
    Thursday, October 26, 2017 5:17 PM
  • Hello Old_School,

    For my class structure, The Rootobject class is a root container that has stored all value from json file, others are served as navigation properties for Rootobject. You don't need to deserialize all classes specified. Just deserialize the Rootobject.

    If you want to populate the property value of other class. Try the below code.

     Rootobject rootobject = Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject>(response);
                var iv=rootobject.result[0].password.crypto.value.iv;        
                var data= rootobject.result[0].password.crypto.value.data;
                string type = rootobject.result[0].password.crypto.type;

    My Class Structure(Notice your json file has invalid character so I take some operation)

     public class Rootobject
        {
            public Result[] result { get; set; }
            public int resultCount { get; set; }
            public object pagedResultsCookie { get; set; }
            public string totalPagedResultsPolicy { get; set; }
            public int totalPagedResults { get; set; }
            public int remainingPagedResults { get; set; }
        }
        public class Result
        {
            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; }
            public string sn { get; set; }
            public string accountStatus { get; set; }
            public string avectraId { get; set; }
            public string AHApw { get; set; }
            public Password password { get; set; }
            public string ahaBirthYear { get; set; }
            public string city { get; set; }
            public string ahaGender { get; set; }
            public string ahaJobTitle { get; set; }
            public string ahaLeadScore { get; set; }
            public string stateProvince { get; set; }
            public string orgId { get; set; }
            public string[] ahaMemberGroup { get; set; }
            public string[] ahaMemberType { get; set; }
            public string regMethod { get; set; }
            public string activationToken { get; set; }
            public string activationTokenTS { get; set; }
            public object[] ahaMemberships { get; set; }
            public object[] access { get; set; }
        }
        public class Password
        {
            //Because the Json file with an invalid character for a field name,
            //So I use JSON attributes to specify the name of the property to map to the model.
            [JsonProperty("$crypto")]
            public Crypto crypto { get; set; }
        }  
         public class Crypto
        {
            public string type { get; set; }
            public Value value { get; set; }
        }
        public class Value
        {
            public string cipher { get; set; }
            public string data { get; set; }
            public string iv { get; set; }
            public string key { get; set; }
        }

    If you have any issues with my reply please feel free to contact me.

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Fei Hu Friday, October 27, 2017 2:50 AM
    • Marked as answer by old_School Tuesday, November 7, 2017 9:46 PM
    Friday, October 27, 2017 2:47 AM
  • Hello old_School,

    Is there anything updated?

    If you have any issues about the replies , please feel free to contact us.

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, October 29, 2017 5:22 AM
  • Sorry for the delay, I got the same answer on stack overflow. Sorry took so long to get back on here and check this as the answer. Thank you for your help.
    Tuesday, November 7, 2017 9:47 PM