How can i access data from public object value c# Windows App

Ahmed Abdo 21 Reputation points
2022-06-12T12:20:23.61+00:00

public class Rootobject
{
public string id { get; set; }
public string id_form { get; set; }
public string name { get; set; }
public string id_creator { get; set; }
public DateTime creation_date { get; set; }
public DateTime modification_date { get; set; }
public Item[] items { get; set; }
}

public class Item  
{  
    public int id_form_item { get; set; }  
    public string item_type { get; set; }  
    public object value { get; set; }  
}
C#
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.
11,194 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,626 Reputation points Microsoft Vendor
    2022-06-16T03:10:11.253+00:00

    @Ahmed Abdo , Welcome to Microsoft Q&A, based on my test, I find that your json file has the problem. Please delete the comment // This value in your json file.

    Then, you could try the following code to access the value by using class object.

      public partial class Form1 : Form  
        {  
            public Form1()  
            {  
                InitializeComponent();  
            }  
      
            private void button1_Click(object sender, EventArgs e)  
            {  
                var myJsonResponse = File.ReadAllText(@"C:\Users\username\Desktop\2.txt");  
                dynamic myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);  
                 textBox1.Text = myDeserializedClass.items[9].value[0][0].id_form_item;  
            }  
        }  
      
      
        public class Item  
        {  
            public int id_form_item { get; set; }  
            public string item_type { get; set; }  
            public object value { get; set; }  
        }  
      
        public class Root  
        {  
            public string id { get; set; }  
            public string id_form { get; set; }  
            public string name { get; set; }  
            public string id_creator { get; set; }  
            public DateTime creation_date { get; set; }  
            public DateTime modification_date { get; set; }  
            public List<Item> items { get; set; }  
        }  
    

    Result:

    211778-image.png

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    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.


3 additional answers

Sort by: Most helpful
  1. Karen Payne MVP 35,466 Reputation points
    2022-06-12T13:21:44.763+00:00

    Use a static class or a Singleton

    Singleton example which is thread safe done with .NET Core, C# 9

    public sealed class RootObject  
    {  
        private static readonly Lazy<RootObject> Lazy = new(() => new RootObject());  
        public static RootObject Instance => Lazy.Value;  
        public int Id { get; set; }  
        public string Name { get; set; }  
    }  
    

    Set a property

    RootObject.Instance.Id = 0;  
    

    Get the property

    SomeId = RootObject.Instance.Id;  
    

    Syntax for .NET Framework

    public sealed class RootObject  
    {  
        private static readonly Lazy<RootObject> Lazy =  
            new Lazy<RootObject>(() =>  
                new RootObject());  
      
        public int Id { get; set; }  
        public string Name { get; set; }  
        public static RootObject Instance => Lazy.Value;  
    }  
    
    0 comments No comments

  2. Ahmed Abdo 21 Reputation points
    2022-06-15T06:48:45.247+00:00

    thank you for your Support

    we get JSON data from API but can't access this value

    211526-jsonfiledata.txt

    211536-value.jpg


  3. Ahmed Abdo 21 Reputation points
    2022-06-16T07:19:09.327+00:00
    0 comments No comments

Your answer

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