Click here to Skip to main content
14,766,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The error I am getting is as follows:

the current JSON array (e.g. [1,2,3]) into type 'Dorman.SpecialOrderSKU.Models.Questions' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

What I have tried:

JSON:
{
    "PartNumber": "123-456",
    "NumberOfQuestions": 5,
    "dateLastModified": "",
    "status": "active",
    "type": "question",
    "Questions": [
        {
            "validationID": 1,
            "QuestionID": 1,
            "Question": "some question text",
            "ResponseType": "Single_Select",
            "Option_List": {
                "1": "option 1 text",
                "2": "option 2 text",
                "3": "option 3 text"
            }
        },
        {
            "validationID": 1,
            "QuestionID": 2,
            "Question": "some question text",
            "ResponseType": "Multi_Select",
            "Option_List": [
                "option 4 text",
                "option 5 text",
                "option 6 text"
            ]
        },
        {
            "validationID": 6,
            "QuestionID": 3,
            "Question": "some question text",
            "ResponseType": "Number"
        },
        {
            "validationID": 2,
            "QuestionID": 4,
            "Question": "some question text",
            "ResponseType": "String"
        },
        {
            "validationID": 3,
            "QuestionID": 5,
            "Question": "some question text",
            "ResponseType": "Boolean"
        }
    ],
    "id": "ab3fcc42-9d5a-48d4-96ea-388c5edd886f",
    "_rid": "HopBAKbRQQ0BAAAAAAAAAA==",
    "_self": "dbs/HopBAA==/colls/HopBAKbRQQ0=/docs/HopBAKbRQQ0BAAAAAAAAAA==/",
    "_etag": "\"2e00b41c-0000-0200-0000-5ed857d40000\"",
    "_attachments": "attachments/",
    "_ts": 1591236564
}


I have used the below class for deserializing

public class QuestionsAnswers
{
    [JsonProperty("PartNumber")]
    public string PartNumber { get; set; }

    [JsonProperty("NumberOfQuestions")]
    public int NumberOfQuestions { get; set; }

    [JsonProperty("dateLastModified")]
    public string DateLastModified { get; set; }

    [JsonProperty("status")]
    public string Status { get; set; }

    [JsonProperty("type")]
    public string Type { get; set; }

    public QuestionList Questions { get; set; }

    [JsonProperty("id")]
    public string ID { get; set; }

    [JsonProperty("_rid")]
    public string Rid { get; set; }

    [JsonProperty("_self")]
    public string Self { get; set; }

    [JsonProperty("_etag")]
    public string Etag { get; set; }

    [JsonProperty("_attachments")]
    public string Attachments { get; set; }

    [JsonProperty("_ts")]
    public int Ts { get; set; }
}

public class QuestionList : ICollection<Questions>
{
    public List<Questions> questionList { get; set; }

    public int Count
    {
        get { return questionList.Count; }
    }

    public bool IsReadOnly
    {
        get
        {
            return false;
        }
    }

    public void Add(Questions item)
    {
        questionList.Add(item);
    }

    public void Clear()
    {
        questionList.Clear();
    }

    public bool Contains(Questions item)
    {
        return questionList.Contains(item);
    }

    public void CopyTo(Questions[] array, int arrayIndex)
    {
        questionList.CopyTo(array, arrayIndex);
    }

    public IEnumerator<Questions> GetEnumerator()
    {
        return questionList.GetEnumerator();
    }

    public bool Remove(Questions item)
    {
        return questionList.Remove(item);
    }

    IEnumerator<Questions> IEnumerable<Questions>.GetEnumerator()
    {
        foreach (Questions ApptReason in questionList)
        {
            yield return ApptReason;
        }
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }

}

public class Questions
{
   // [JsonProperty("validationID")]
    public int ValidationID { get; set; }

  //  [JsonProperty("QuestionID")]
    public int QuestionID { get; set; }

   // [JsonProperty("Question")]
    public string Question { get; set; }

 //   [JsonProperty("ResponseType")]
    public string ResponseType { get; set; }

    //[JsonProperty("Option_List")]
    //public Dictionary<string, string> OptionList { get; set; }
}


Can some one help me on this
Posted
Updated 5-Jun-20 4:24am

1 solution

Your code worked ok for me except I had to add a constructor to the QuestionList class

public QuestionList()
{
    this.questionList = new List<Questions>();
}


Not because I was getting the error you were though, so if you still get that error the problem may lie elsewhere.

For reference this was my calling code;

var qa = JsonConvert.DeserializeObject<QuestionsAnswers>(json);
   
Comments
Member 14836690 5-Jun-20 9:31am
   
Thanks for the update. but unfortunately after updating the constructor it didn't worked. Here is the error message that i got "Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Dorman.SpecialOrderSKU.Models.Questions' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<t> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.'
"
F-ES Sitecore 5-Jun-20 10:12am
   
Then your json isn't as you posted, it will be an array so will start and end in square brackets;

[{...}, {...}]

in that case you need to deserialise to a List

var qa = JsonConvert.DeserializeObject<List<QuestionsAnswers>>(json);
F-ES Sitecore 5-Jun-20 10:59am
   
You're defining option_list two different ways in the same json, you're going to have to pick one format and stick with it. If you use

"Option_List": [
"option 1 text",
"option 2 text",
"option 3 text"
]

then define option_list as

[JsonProperty("Option_List")]
public List<string> OptionList { get; set; }

If you use

"Option_List": {
"1": "option 4 text",
"2": "option 5 text",
"3": "option 6 text"
}

then

[JsonProperty("Option_List")]
public Dictionary<string, string> OptionList { get; set; }
Member 14836690 5-Jun-20 11:03am
   
Thanks for the update F-ES Sitecore. You saved my day

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900