none
How to read JSON from C# RRS feed

  • Question

  • I have difficulty in reading the following JSON format in C#,as the attributes changes inside each section.Can somebody help me on this? 

    [{

        "Package": [{
            "p_make": "hy",
            "p_maketype": "1",
            "p_relation": "P111"
            
        },{
            "p_make": "hz",
            "p_maketype": "1",
            "p_relation": "P111"
            
        }],
    "Product": "P1"
    },
    {
    "Product": "P2",
    "Package": [{
            "p_make": "ha",
            "p_maketype": "2",
            "p_relation": "P112"
            
        },
    {
            "p_make": "hb",
            "p_maketype": "2",
            "p_relation": "P112"
            
        },
    {
            "p_make": "hc",
            "p_maketype": "2",
            "p_relation": "P112"
            
        }
    ],
    }
    ]

    Regards,

    Raj

    Tuesday, April 17, 2018 1:13 PM

Answers

  • If you're using a JSON parser like JSON.NET then it can read arbitrary JSON into data so you can work with it. But the JSON seems simple to me. You have an array of items (products). Each product has a name (Product) and a set of packages associated with it. Each package has a make, maketype and relation. That can be easily serialized/deserialized with something like this.

    class Program
    {
        static void Main ( string[] args )
        {
            var json = File.ReadAllText("test.json");
    
            var results = JsonConvert.DeserializeObject<IEnumerable<ProductInfo>>(json);
        }
    }
    
    class ProductInfo
    {
        public string Product { get; set; }
    
        public IEnumerable<ProductPackage> Package { get; set; }
    }
    
    class ProductPackage
    {
        public string p_make { get; set; }
        public string p_maketype { get; set; }
    
        public string p_relation { get; set; }
    }

    The above code uses Newtonsoft.Json to read the data. I personally don't like using attributes on the DTOs to fix the names in the JSON but you could.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, April 17, 2018 2:04 PM
    Moderator

All replies

  • hmm, please post a concise and complete example as console application. And rephrasing your question, adding some context would be helpful.

    Cause JSON is a string. Which is simple to read in C#.

    Tuesday, April 17, 2018 1:24 PM
  • Sorry for the partial info.

    What am trying to achieve is read the json file and create a excel sheet out of it.

    Like the one below

    p_make  p_maketype    p_relation  Product

    hy                 1                    P111         P1

    hz                 1                    P111         P1

    ha                 2                   P112          P2

    hb                 2                   P112          P2

    hc                 2                   P112          P2

    Tuesday, April 17, 2018 1:49 PM
  • If you're using a JSON parser like JSON.NET then it can read arbitrary JSON into data so you can work with it. But the JSON seems simple to me. You have an array of items (products). Each product has a name (Product) and a set of packages associated with it. Each package has a make, maketype and relation. That can be easily serialized/deserialized with something like this.

    class Program
    {
        static void Main ( string[] args )
        {
            var json = File.ReadAllText("test.json");
    
            var results = JsonConvert.DeserializeObject<IEnumerable<ProductInfo>>(json);
        }
    }
    
    class ProductInfo
    {
        public string Product { get; set; }
    
        public IEnumerable<ProductPackage> Package { get; set; }
    }
    
    class ProductPackage
    {
        public string p_make { get; set; }
        public string p_maketype { get; set; }
    
        public string p_relation { get; set; }
    }

    The above code uses Newtonsoft.Json to read the data. I personally don't like using attributes on the DTOs to fix the names in the JSON but you could.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, April 17, 2018 2:04 PM
    Moderator
  • Copy valid JSON data to the clipboard, then go to a C# file (.cs file) in Visual Studio and use "Edit" | "Paste Special" | "Paste JDON As Clasess". When I do that with your data I get:

    public class Rootobject
    {
        public Class1[] Property1 { get; set; }
    }
    
    public class Class1
    {
        public Package[] Package { get; set; }
        public string Product { get; set; }
    }
    
    public class Package
    {
        public string p_make { get; set; }
        public string p_maketype { get; set; }
        public string p_relation { get; set; }
    }

    You can use those classes to read the JSON data using Newtonsoft.Json. You don't say if your application is a Windows Forms or UWP or some other type of application; if you were to be more precise then the answers might be more useful.



    Sam Hobbs
    SimpleSamples.Info

    • Proposed as answer by Simple Samples Tuesday, April 17, 2018 8:12 PM
    Tuesday, April 17, 2018 5:56 PM
  • simply read your file as text file.



    string text = System.IO.File.ReadAllText(@"your file complete path with ext like .json");

    create data structure as below

    class ProductPackage
    {
        public string p_make { get; set; }
        public string p_maketype { get; set; }

        public string p_relation { get; set; }
    }

    class ProductInfo
    {
        public string Product { get; set; }

        public ProductPackage Package { get; set; }
    }

    then

    ProductInfo ProductInfoObj = JsonConvert.DeserializeObject<ProductInfo>(strjson);

    then simple write object information into excel.

     

    Tuesday, April 17, 2018 7:09 PM
  • Thanks.It worked.
    Tuesday, April 17, 2018 10:49 PM