Click here to Skip to main content
15,881,831 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When I Convert the string To Json Object Then Error This type of Error Show.

Cannot deserialize the current json object because(e.g.{"name":"value"}) into type because the type requires a json array (e.g.[1, 2, 3])

error :

Error converting value "38322c46-2f73-42b7-9244-39081991d46f" to type 'Myproject.data.Core.divisionIds'. Path 'conversations[0].divisionIds[0]', line 1, position 220.


What I have tried:

Json
XML
{
  "conversations": [
    {
      "originatingDirection": "inbound",
      "conversationEnd": "2021-03-18T12:39:57.587Z",
      "conversationId": "46f7cda7-ad75-4a1e-b28c-b1c8c01e609e",
      "conversationStart": "2021-03-18T12:39:01.206Z",
      "divisionIds": [
        "aaabbbba-ertw-cldl-2021-547fff22ff33",
        "ddfrgfsc-c3e4-a8f9-b28c-b1c8c01e609e"
      ]
      }]
      }


C#
public class conversations
    {
        public List<divisionIds>  divisionIds { get; set; }

    }

    public class divisionIds
    {
        public string divisionId { get; set; }
    }

C#
responseHeader = JsonConvert.DeserializeObject<ConversationsList>(response.Content);
                if (responseHeader.totalHits > 0)
                {
                    ConversationsList ConversationList = JsonConvert.DeserializeObject<ConversationsList>(response.Content);
                    InsertConversation(ConversationList);
                }
Posted
Updated 28-Sep-22 5:01am
Comments
Richard MacCutchan 28-Sep-22 10:57am    
The divisionIds in the JSON is an array of strings. But you class definitions have it as a single string. Also, I do not think it is a good idea to use the same name for your list as you have for the class.

Start by taking your JSON and running it through an online JSON-to-C# converter, like this one: Convert JSON to C# Classes Online - Json2CSharp Toolkit[^]

What you get are classes that work with that JSON data:
C#
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
    public class Conversation
    {
        public string originatingDirection { get; set; }
        public DateTime conversationEnd { get; set; }
        public string conversationId { get; set; }
        public DateTime conversationStart { get; set; }
        public List<string> divisionIds { get; set; }
    }

    public class Root
    {
        public List<Conversation> conversations { get; set; }
    }
As you can see, the Root object contains a collection rather than a single object - because that is what your JSON data is describing!

You can either remove the collection from the source JSON (not recommended) or access the collection to get the actual data object(s).
 
Share this answer
 
Comments
Richard MacCutchan 28-Sep-22 11:30am    
Does a List<T> work the same as a simple array?
OriginalGriff 28-Sep-22 11:53am    
JSON doesn't have a lot of collections - just the array. But every implementation I have seen works with List or Array as the class. Obviously a List is more flexible, so most converters specify that. Didn't know the VS one didn't - but I don't use it much, if at all.
This is what Visual Studio created from the JSON text above:
JSON
public class Rootobject
 {
     public Conversation[] conversations { get; set; }
 }

 public class Conversation
 {
     public string originatingDirection { get; set; }
     public DateTime conversationEnd { get; set; }
     public string conversationId { get; set; }
     public DateTime conversationStart { get; set; }
     public string[] divisionIds { get; set; }
 }
 
Share this answer
 

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