Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • JSON Issue - In Which Class Do I Need to Add Array Title?

    • 0
    • 0
    • 0
    • 4
    • 0
    • 0
    • 0
    • 855
    Answer it

    Hi, i am very new to this, so please forgive me for stupid mistakes.

     

    I am to create an android application that consume web services currently from IIS. Program that i am currently using: SQLserver , Visual studio for web, android studio. So far connection from sql to the webservice are done the display of my JSON data seems very different from others.

     

    This is how my Json looks like EXAMPLE

     

    [{"a":"1","b":"1"},{"a":"2","b":"2"},{"a":"3","b":"3"},{"a":"4","b":"4"},{"a":"5","b":"15"}]

     

    and many said that this is not Json.

     

    I would like to add in a title and make my json become like EXAMPLE

     

    {"item":[{"a":"1","b":"1","a":"2","b":"2","a":"3","b":"3","a":"4","b":"14,"a":"5","b":"15"}]}

     

    This is the service that display all the json from sql to browser

     

    controller class

    namespace ProductServiceFinal.Controllers
    {
        public class ProductController : ApiController
        {         
    
            public IEnumerable<product> Get()
            {
    
                using (estocktakeEntities entities = new estocktakeEntities())
                {
    
                    
                    return entities.products.ToList();
    
                }
    
    
            }
      }           

     

    product class

       public partial class product
        {
            public string ib_itemcode1 { get; set; }
            public string transtatuscode { get; set; }
            public string invtid { get; set; }
            public string descr { get; set; }
        }

     

    data context class

        public partial class estocktakeEntities : DbContext
        {
            public estocktakeEntities()
                : base("name=estocktakeEntities")
            {
            }
        
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                throw new UnintentionalCodeFirstException();
            }
        
            public virtual DbSet<product> products { get; set; }
        }
    }

     

    I believe that i should add the array title somewhere. Maybe in the product class but i am unsure about how to go about it. 

     

    Any suggestion will be a great help.

     

    Thank you

 4 Answer(s)

  • Hi Phan Kiah How,

    hope you are doing good. As far as your question is concerned it is perfectly fine :).

    What I have understood from your question is that you are creating an android application which consumes Web API created in ASP.NET MVC. The API is providing data in JSON format and you are showing that JSON data in browser. You have a Product class where you want to add a Title field which is actually an array consisting of "a" and "b" fields.

    Few points (which include answer to your question) that I would like to mention are as follows:
     
    1. If you feel that you need a variable and it relates to any of the class in your code then you should add that variable to the related class only. Suppose I have a class called "Person" and "Vehicle", and I thought of adding an address for a "Person" then it is clear that I need to add "Address" field to "Person" class and not to other ones. So in your case if "Title" is related to "Product" then you should add "Title" field to "Product" class.
    2. To check if your JSON is valid or not you would like to check it using inbuilt tools, which helps you in saving your time. You may like to use http://json2csharp.com/ for the same. Just paste your JSON string in the provided field and click Generate to see equivalent class in C#. In case if provided JSON is invalid it will let you know about it.
    3. To make your code more readable and manageable you would like to implement naming conventions in C# code. Please refer to http://www.dofactory.com/reference/csharp-coding-standards or any other good site of your choice.
    4. To learn more about JSON (JavaScript Object Notation) you might like to check out good tutorials, for example http://www.javatpoint.com/json-example

    Now let's see the solution to your query.

    The valid JSON for title array would be
     
    {"title":
    	[
    		{"a":"1", "b":"1"},
    		{"a":"2", "b":"2"},
    		{"a":"3", "b":"3"},
    		{"a":"4", "b":"14"},
    		{"a":"5", "b":"15"}
    	]
    }

    and your Product class with Title property would be 
     
    public partial class Product
    {
        public string Ib_ItemCode1 { get; set; }
        public string TranStatusCode { get; set; }
        public string InvtId { get; set; }
        public string Descr { get; set; }
        public List<Title> Title { get; set; }
    }
    
    public class Title
    {
        public string A { get; set; }
        public string B { get; set; }
    }

    Please note that now your Product class has a Title field which is of type Title. Class Title has 2 properties A and B which means that every time you create an object of Product type it will have a Title property which further represent properties A and B. 

    Please let me know if there is any concern. :)
     
  • Thanks guys for the help so these are the changes i had done.

    added a new class Title
    namespace ProductDataAccess
    {
        using System;
        using System.Collections.Generic;
    
        public partial class product
        {
            public string ib_itemcode1 { get; set; }
            public string transtatuscode { get; set; }
            public string invtid { get; set; }
            public string descr { get; set; }
            public List<Title> title { get; set; }
    
        }
       //added below
        public class Title
            {
    
                public string ib_itemcode1 { get; set; }
                public string transtatuscode { get; set; }
                public string invtid { get; set; }
                public string descr { get; set; }
            }    
    
    }
    

    next i update my DB context by adding in
    namespace ProductDataAccess
    {
        using System;
        using System.Data.Entity;
        using System.Data.Entity.Infrastructure;
        
        public partial class estocktakeEntities : DbContext
        {
            public estocktakeEntities()
                : base("name=estocktakeEntities")
            {
            }
        
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                throw new UnintentionalCodeFirstException();
            }
        
            public virtual DbSet<product> products { get; set; }
            public virtual DbSet<Title> titles { get; set; }
    
    
        }
    }
    
    Lastly i edit my controller to pull data from the correct class.
    namespace ProductServiceFinal.Controllers
    {
        public class ProductController : ApiController
        {
    
            public IEnumerable<Title> Get()
            {
    
                using (estocktakeEntities entities = new estocktakeEntities())
                {
    
                    
                    return entities.titles.ToList();
    
                }
    
    
            }
                 
     
    .
    however when i enter the url to get all the data. i get this error instead. 

    An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

    Additional information: The entity type Title is not part of the model for the current context. 

    it is pointing on this line of code. 

                    return entities.titles.ToList();

    from what i can understand, you can to list() anything from titles, as it consist of other data in the product class. but how do i access it? 

    Thanks for the time and trouble
     
  • Hi,

    First of all, the given below JSON format is incorrect:
    {"item":[{"a":"1","b":"1","a":"2","b":"2","a":"3","b":"3","a":"4","b":"14,"a":"5","b":"15"}]}
    Because every curly braces{} cannot contain the same property. In above JSON, property "a" and "b" are repeatedly available in same curly braces.

    The following given below JSON is correct
    [{"a":"1","b":"1"},{"a":"2","b":"2"},{"a":"3","b":"3"},{"a":"4","b":"4"},{"a":"5","b":"15"}]
    If you want to add "item" as a title in above JSON, for example-
    {"item":[{"a":"1","b":"1"},{"a":"2","b":"2"},{"a":"3","b":"3"},{"a":"4","b":"4"},{"a":"5","b":"15"}]}
    Then, you can use following classes:
    public class Item //The class name will be display as title in JSON
    {
        public string a { get; set; }
        public string b { get; set; }
    }
    
    public class RootObject
    {
        public List<Item> item { get; set; }
    }
    Thanks.
     
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: