question

MIndraRuslan-8520 avatar image
0 Votes"
MIndraRuslan-8520 asked DanielZhang-MSFT commented

Deserialize json file and place the data to textbox

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

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MIndraRuslan-8520 avatar image
0 Votes"
MIndraRuslan-8520 answered DanielZhang-MSFT commented

Hi Daniel,

Many thanks for the help.
your codes work.

Thanks

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @MIndraRuslan-8520,
I am happy to help you solve the problem.
And if my answer is helpful, please click "Accept Answer" instead of your comment.
Best Regards,
Daniel Zhang

0 Votes 0 ·
DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MIndraRuslan-8520 avatar image
0 Votes"
MIndraRuslan-8520 answered DanielZhang-MSFT commented

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'

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @MIndraRuslan-8520,
The code is working well as follows:
89056-419.gif
Which line of code did you encounter the error?
Please provide more information and some code to reproduce the situation.
Best Regards,
Daniel Zhang


0 Votes 0 ·
419.gif (69.0 KiB)
MIndraRuslan-8520 avatar image
0 Votes"
MIndraRuslan-8520 answered DanielZhang-MSFT edited

Actually, there are 9 items, see the following image

89131-image.png



image.png (58.3 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @MIndraRuslan-8520,
I added items to my test.json and made a test.
Here is my test.json file information.
89170-4201.png
And test result:
89274-420.png
So does your json file contain nine items? Please provide specific information about your json file and code to reproduce the problem.
Best Regards,
Daniel Zhang


0 Votes 0 ·
4201.png (23.3 KiB)
420.png (8.3 KiB)
MIndraRuslan-8520 avatar image
0 Votes"
MIndraRuslan-8520 answered

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

89248-image.png


89401-image.png



image.png (72.0 KiB)
image.png (92.8 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.