-
JSON Issue - In Which Class Do I Need to Add Array Title?
over 7 years ago
-
over 7 years ago
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:
- 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.
- 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.
- 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.
- 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. :)
-
over 7 years ago
Hi Phan Kiah How,
Please consider the following changes
- If Title class doesn't need to have repetitive properties like in Product class then please remove them.
- As you are using Code First approach in Entity Framework, change following line in Product class definition
public List<Title> title { get; set; }
public virtual ICollection<Title> title { get; set; }
- Also you may keep the following line in ProductController
return entities.products.ToList();
return entities.titles.ToList();
To save you further trouble, I would suggest you to go through a simple Entity Framework Code First tutorial before trying your hands on code. Once you are okay with the language and the code pattern then relate it to what you are trying at your end.
Few links that would be useful to you are as follows. In the following links you may like to focus on how relations are maintained between entities.- http://www.entityframeworktutorial.net/code-first/simple-code-first-example.aspx
- https://msdn.microsoft.com/en-us/library/jj193542(v=vs.113).aspx
- https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
-
-
over 7 years ago
Thanks guys for the help so these are the changes i had done.
added a new class Titlenamespace 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 innamespace 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; } } }
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.
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
-
-
over 7 years ago
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"}]}
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"}]
{"item":[{"a":"1","b":"1"},{"a":"2","b":"2"},{"a":"3","b":"3"},{"a":"4","b":"4"},{"a":"5","b":"15"}]}
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; } }
-
4 Answer(s)