skip to Main Content

I’m trying to deserialize following Json. I am getting this Json from a server and have to use it on a desktop application

{
"panicForms": [{
    "name": "Name",
    "description": "Description",
    "title": "Title",
    "Image": "",
    "keyboardShortcut": "",
    "instructions": "Instructions",
    "submitButtonText": "",
    "label": "",
    "tooltip": "",
    "textColor": "Black",
    "backgroundColor": "Red",
    "textStyle": "Normal",
    "systemTrayEnabled": true,
    "fields": [{
            "eventField": "Name",
            "label": "Full Name",
            "type": "single selection",
            "required": true
        },
        {
            "eventField": "Age",
            "label": "age",
            "type": "single selection",
            "required": false
        }
    ]
}]

}

Created following objects

// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Field
{
    public string eventField { get; set; }
    public string label { get; set; }
    public string type { get; set; }
    public bool required { get; set; }
}

public class PanicForm
{
    public string name { get; set; }
    public string description { get; set; }
    public string title { get; set; }
    public string Image { get; set; }
    public string keyboardShortcut { get; set; }
    public string instructions { get; set; }
    public string submitButtonText { get; set; }
    public string label { get; set; }
    public string tooltip { get; set; }
    public string textColor { get; set; }
    public string backgroundColor { get; set; }
    public string textStyle { get; set; }
    public bool systemTrayEnabled { get; set; }
    public List<Field> fields { get; set; }
}

public class Root
{
    public List<PanicForm> panicForms { get; set; }
}

and used tried following code to deserialize

var stuff1 = JsonConvert.DeserializeObject<PanicForm[]>(jsonString1);

&

var stuff1 = JsonConvert.DeserializeObject<List<PanicForm>>(jsonString1);

&

var stuff1 = JsonConvert.DeserializeObject<Root>(jsonString1);

&

var stuff1 = JsonConvert.DeserializeObject<PanicForm>(jsonString1);

&

Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(jsonString1);

but in the end getting the same error

Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: s. Path '', line 0, position 0.'

detail is

        StackTrace  "   at Newtonsoft.Json.JsonTextReader.ParseValue()rn   at Newtonsoft.Json.JsonTextReader.Read()rn   at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter)rn   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)rn   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)rn   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)rn   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)rn   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)rn   at TestDynamicControls.Form1..ctor() in C:\Alertus\PanicUI\TestDynamicControls\TestDynamicControls\Form1.cs:line 58rn   at TestDynamicControls.Program.Main() in C:\Alertus\PanicUI\TestDynamicControls\TestDynamicControls\Program.cs:line 19"  string

Can any one help me in this solving this, thanks

2

Answers


  1. For the data in the example you exposed:

    The right way is told in a comment by the tool you used:

    Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 
    

    Check this fiddle:

    https://dotnetfiddle.net/vofLpq

    For the classes:

    public class Field
    {
        public string eventField { get; set; }
        public string label { get; set; }
        public string type { get; set; }
        public bool required { get; set; }
    }
    
    public class PanicForm
    {
        public string name { get; set; }
        public string description { get; set; }
        public string title { get; set; }
        public string Image { get; set; }
        public string keyboardShortcut { get; set; }
        public string instructions { get; set; }
        public string submitButtonText { get; set; }
        public string label { get; set; }
        public string tooltip { get; set; }
        public string textColor { get; set; }
        public string backgroundColor { get; set; }
        public string textStyle { get; set; }
        public bool systemTrayEnabled { get; set; }
        public List<Field> fields { get; set; }
    }
    
    public class Root
    {
        public List<PanicForm> panicForms { get; set; }
    }
    

    you have to deserialize:

        Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
    

    with this references:

    using System.Collections.Generic;
    using Newtonsoft.Json;
    

    If you are having data problems you should check your input first, you could use any json validator as: https://jsonlint.com/

    Login or Signup to reply.
  2. To get a list , you can parse a json string, and after this to deserilize an array

    List<PanicForm> panicForms = JObject.Parse(jsonString1)["panicForms"].ToObject<List<PanicForm>>();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search