logo
down
shadow

Why getting error Cannot deserialize the current JSON object?


Why getting error Cannot deserialize the current JSON object?

By : Nas
Date : November 20 2020, 11:01 PM
Hope that helps Your JSON represents a single object, while your code is trying to deserialize into an array. That won't work.
Change this line:
code :
GridCtrlDocumenti.DataSource = JsonConvert.DeserializeObject(Of Documenti())(DocumentisonData).ToList()
Dim list As List(Of Documenti) = New List(Of Documenti)
list.Add(JsonConvert.DeserializeObject(Of Documenti)(DocumentisonData))
GridCtrlDocumenti.DataSource = list


Share : facebook icon twitter icon
Cannot deserialize the current JSON object into type 'WindowsService1...requires a JSON array (e.g. [1,2,3]) to deserial

Cannot deserialize the current JSON object into type 'WindowsService1...requires a JSON array (e.g. [1,2,3]) to deserial


By : user2818258
Date : March 29 2020, 07:55 AM
I wish this helpful for you Since current_call is sometimes an object, sometimes an array and sometimes not there at all, you will need to use a JsonConverter in order to avoid errors. You can use the SingleOrArrayConverter converter from this similar question and combine it with a null check to get a workable solution. Here is a demo that covers all three scenarios:
code :
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("--- array of calls ---");
        DeserializeAndDumpCallsList(@"
        {
            ""response"": {
                ""method"": ""switchvox.currentCalls.getList"",
                ""result"": {
                    ""current_calls"": {
                        ""current_call"": [
                            {
                                ""provider"": ""ThinkTel"",
                                ""start_time"": ""2014-11-30 02:24:52"",
                                ""duration"": ""5"",
                                ""to_caller_id_number"": ""800"",
                                ""state"": ""ivr"",
                                ""from_caller_id_name"": ""<unknown>"",
                                ""to_caller_id_name"": ""Main Answer Queue"",
                                ""format"": ""ulaw"",
                                ""from_caller_id_number"": ""9999999999"",
                                ""id"": ""SIP/1234567890-08682a00""
                            },
                            {
                                ""provider"": ""ThinkTel"",
                                ""start_time"": ""2014-11-30 02:24:50"",
                                ""duration"": ""7"",
                                ""to_caller_id_number"": ""800"",
                                ""state"": ""ivr"",
                                ""from_caller_id_name"": ""<unknown>"",
                                ""to_caller_id_name"": ""Main Answer Queue"",
                                ""format"": ""ulaw"",
                                ""from_caller_id_number"": ""1111111111"",
                                ""id"": ""SIP/9876543210-08681350""
                            }
                        ],
                        ""total_items"": ""2""
                    }
                }
            }
        }");

        Console.WriteLine("--- single call ---");
        DeserializeAndDumpCallsList(@"
        {
            ""response"": {
                ""method"": ""switchvox.currentCalls.getList"",
                ""result"": {
                    ""current_calls"": {
                        ""current_call"": {
                            ""provider"": ""Internal"",
                            ""start_time"": ""2014-11-30 19:15:44"",
                            ""duration"": ""250"",
                            ""to_caller_id_number"": ""777"",
                            ""state"": ""talking"",
                            ""from_caller_id_name"": ""<unknown>"",
                            ""to_caller_id_name"": ""<unknown>"",
                            ""format"": ""ulaw->ulaw"",
                            ""from_caller_id_number"": ""231"",
                            ""id"": ""SIP/231-b4066e90""
                        },
                        ""total_items"": ""1""
                    }
                }
            }
        }");

        Console.WriteLine("--- no current call ---");
        DeserializeAndDumpCallsList(@"
        {
            ""response"": {
                ""method"": ""switchvox.currentCalls.getList"",
                ""result"": {
                    ""current_calls"": {
                        ""total_items"": ""0""
                    }
                }
            }
        }");
    }

    private static void DeserializeAndDumpCallsList(string json)
    {
        var root = JsonConvert.DeserializeObject<RootObject>(json);
        List<CurrentCall> calls = root.response.result.current_calls.current_call;

        if (calls != null)
        {
            foreach (CurrentCall call in calls)
            {
                Console.WriteLine(call.from_caller_id_number);
            }
        }
        else
        {
            Console.WriteLine("no calls");
        }

        Console.WriteLine();
    }

    public class RootObject
    {
        public Response response { get; set; }
    }

    public class Response
    {
        public string method { get; set; }
        public Result result { get; set; }
    }

    public class Result
    {
        public CurrentCalls current_calls { get; set; }
    }

    public class CurrentCalls
    {
        [JsonConverter(typeof(SingleOrArrayConverter<CurrentCall>))]
        public List<CurrentCall> current_call { get; set; }
        public string total_items { get; set; }
    }

    public class CurrentCall
    {
        public string provider { get; set; }
        public string start_time { get; set; }
        public string duration { get; set; }
        public string to_caller_id_number { get; set; }
        public string state { get; set; }
        public string from_caller_id_name { get; set; }
        public string to_caller_id_name { get; set; }
        public string format { get; set; }
        public string from_caller_id_number { get; set; }
        public string id { get; set; }
    }

    class SingleOrArrayConverter<T> : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return (objectType == typeof(List<T>));
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JToken token = JToken.Load(reader);
            if (token.Type == JTokenType.Array)
            {
                return token.ToObject<List<T>>();
            }
            return new List<T> { token.ToObject<T>() };
        }

        public override bool CanWrite
        {
            get { return false; }
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }
    }
}
--- array of calls ---
9999999999
1111111111

--- single call ---
231

--- no current call ---
no calls
Cannot deserialize the current JSON object error from IMGUR API in C#

Cannot deserialize the current JSON object error from IMGUR API in C#


By : adie bontenah
Date : March 29 2020, 07:55 AM
this will help The value for albumlink that you're passing is incorrect, it should not have the #0 on the end.
The value for albumlink should just be Dc2k6.
Converting JSON to Object fails - Cannot deserialize the current JSON object into System.Collections.Generic.List

Converting JSON to Object fails - Cannot deserialize the current JSON object into System.Collections.Generic.List


By : Koen van mullem
Date : March 29 2020, 07:55 AM
will help you First of all you have to figure out what your API returns.
Right now you're trying to parse a List of jsonToObj Arrays (List). You have to decide whether to use a jsonToObj[] or List or a simple jsonToObj which your API provides now:
code :
var a = JsonConvert.DeserializeObject<jsonToObj>(richTextBox1.Text);
public class messages
{
    public string id { get; set; }
    public long recipient { get; set; }
}
public class jsonToObj
{
  ...
  public string[] inDND { get; set; }
  ...
}
VB.NET Deserialize JSON - Cannot deserialize the current JSON object into type .Customer[]' because the type requires a

VB.NET Deserialize JSON - Cannot deserialize the current JSON object into type .Customer[]' because the type requires a


By : user1927184
Date : March 29 2020, 07:55 AM
Any of those help You are getting this exception because you have omitted the Property keyword from a couple of the members in your classes, which changes the semantics of the parentheses.
For example, in your CustomerWrapper class, you have declared the customers member like this:
code :
Public customers() As Customer
Public customers As Customer()   ' Field - array of Customer
Public Property customers() As Customer   ' Property - single Customer
Public Class CustomerWrapper
    Public Property customers As Customer
End Class

Public Class Metadata
    Public Property id As String
End Class

Public Class Customer
    Public Property id As String
    Public Property created_at As String
    Public Property email As String
    Public Property given_name As String
    Public Property family_name As String
    Public Property address_line1 As String
    Public Property address_line2 As String
    Public Property address_line3 As String
    Public Property city As String
    Public Property region As String
    Public Property postal_code As String
    Public Property country_code As String
    Public Property language As String
    Public Property phone_number As String
    Public Property metadata As Metadata
End Class
De serialization of object is giving error : Cannot deserialize the current JSON object?

De serialization of object is giving error : Cannot deserialize the current JSON object?


By : user1976708
Date : March 29 2020, 07:55 AM
Hope that helps The problem is caused by how the SearchAsync method of the ForceClient deserializes the response.
The client attempts to deserialize the response from the server as a List but, as we see in the JSON, the response is wrapped in a searchRecords element first so the deserialization throws the exception you are seeing.
code :
var response = await client.SearchAsync<dynamic>(query);
var records = JsonConvert.DeserializeObject<List<SearchRecord>>(list.searchRecords.ToString());
Related Posts Related Posts :
  • Map an object to an object with a sub list
  • .NET Core get connection string from appsettings.json
  • Where can i find some themes/demo for UI in WPF application?
  • Console Application to retrieve Domain Shared Contacts (Google Apps Admin SDK)
  • How combine two different Paths
  • EntityFramework-Plus and ASP.NET Memory Cache expiration
  • How to pre-populate vimeo password protected video in the website
  • An exception of type 'System.ArgumentNullException' occurred in Microsoft.AspNet.Identity.EntityFramework.dll
  • Getting the error message: “Could not find a part of the path"
  • Race conditition when using Process RedirectStandardError and RedirectStandardOutput,
  • How do I use an array from code in the xaml file? C#
  • How do I insert data when the primary key column is not an identity column?
  • recursively step through asp table (that has been populated with a query)
  • Redirect to login when unauthorized in ASP.NET Core
  • What is the difference between a RibbonWindow and Window?
  • Picking random objects from class
  • How to link to a header file in a C# using Visual Studio
  • Why GetEntryAssembly returns null?
  • Setting focus to textbox upon method-execution
  • How to get Column values stored in session datatable?
  • Asynchronous error handling in MVC 5 filters
  • Unable to animate the character in unity3d
  • C# Generic to Session
  • Change keys Extended WPF Toolkit Zoombox
  • At least one object must implement IComparable order by
  • What is the reference that need to be added for "using Microsoft.Web.WebPages.OAuth" to get facebook authentic
  • Windows 10 - Background Task write to App's Calendar
  • Custom helper works with input type but not with Html helper
  • How to get the guid of a process that is running
  • UWP - Template10 pageheader ellipsis tabstop on version 1.1.12
  • Check id from two tables, if there is no match delete it
  • Counting how many occurences have been between datetimes
  • Xamarin.form - Masterdetailpage : System.Reflection.TargetInvocationException: Exception has been thrown by the target o
  • Add asp.net LinkButton to a Literal using c#
  • Xamarin.Forms (WinPhone) Hide the Title space NavigationBar for Xamarin.Forms.NavigationPage. Please
  • Get connection string in .NET Core application
  • C# import dll function with const string& as parameter
  • How to navigate from browser to application in Citrix
  • Create buttons dynamically from code behind in ASP.NET
  • Filling datatable with int array.
  • Post data using WebClient in C# and server replay Bad Gateway
  • Collections Generic List anonymous type to System Data.IDataReader
  • How to call async method from not async method?
  • c# compare two lists based on sub items and return the difference
  • Entity Framework: Navigation Property from a service
  • Can't declare Session variable in asp.net sharepoint page
  • How to intercept filterchain for maintenance in a WebApi project
  • Entity Framework - Use Dynamic connection string with Oracle Provider
  • Inconsistent Convert.ToDouble(decimal) results
  • Get Child Directory from Path
  • Unable to resize StackPanel when IDataErrorInfo is triggered
  • How to stop "Update Background Files"?
  • Proper way to use dependency injection when some arguments will be known only at runtime
  • How to remove duplicate List in my List<List<int>> object?
  • Unable to cast object of type 'MongoDB.Bson.Serialization.Serializers.DateTimeSerializer' to type 'MongoDB.Bson.Serializ
  • Lightswitch submit a form on enter key
  • Business Logic in the JavaScript
  • tcp socket asynchronous communication
  • Syntax match using regex in c#
  • Display timerpickerdialog within a fragment using Xamarin Android
  • shadow
    Privacy Policy - Terms - Contact Us © soohba.com