Deserialize json file and place the data to textbox

M Indra Ruslan 241 Reputation points
2021-04-16T06:13:47.28+00:00

Hi

I have a JSON file with the following information :
{
"WindowsCustomization": [
{
"id": 1,
"Item": "Disable Windows 10 Tablet Mode, Use desktop mode and Don't ask me and don't switch.",
"value": "True"
},
{
"id": 2,
"Item": "Show the touch keyboard when not in tablet mode and there's no keyboard attached.",
"value": "True"
}
]
}

I have a problem with Deserialize it using Newtonsoft and place the item value to the label text.
Is anybody can help me with this? Thanks in advance

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. M Indra Ruslan 241 Reputation points
    2021-04-20T08:10:30.603+00:00

    Hi Daniel,

    Many thanks for the help.
    your codes work.

    Thanks


4 additional answers

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,661 Reputation points
    2021-04-16T09:22:59.007+00:00

    Hi MIndraRuslan-8520,
    First, you need to create the class, which represents the data in JSON.
    I created two classes: the WCustomizationis for the outer fields (WindowsCustomization) and a list of objects of type Item.
    Here is my test code you can refer to.

    private void Form1_Load(object sender, EventArgs e)  
    {  
        string jsonFile = File.ReadAllText(@"C:\Users\Desktop\test.json");//change to your json file  
        var json= JsonConvert.DeserializeObject<WCustomization>(jsonFile);     
        label1.Text = json.WindowsCustomization[0].Item;  
        label2.Text= json.WindowsCustomization[1].Item;  
      
    }  
    }  
    public class items  
    {  
    public int id { get; set; }  
    public string Item { get; set; }  
    public string value { get; set; }  
             
    }  
    class WCustomization  
    {  
              
    public List<items> WindowsCustomization { get; set; }  
    }  
    

    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. M Indra Ruslan 241 Reputation points
    2021-04-19T08:56:30.407+00:00

    Hi Daniel,

    Thanks for your reply, but there is a problem. I got the following issue :
    "System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index'


  3. M Indra Ruslan 241 Reputation points
    2021-04-19T10:47:05.957+00:00

    Actually, there are 9 items, see the following image

    89131-image.png


  4. M Indra Ruslan 241 Reputation points
    2021-04-20T04:52:47.33+00:00

    Hi Daniel,
    Thanks for your help, please see the following information.
    I post as well my code, thanks.

    89248-image.png

    89401-image.png

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.