Use Json file as db - c#

I am starting c# and I was asked to create a small Api using a given Json file as DB.
My json file looks like:
{"Items": [
{
"id": 1,
"name": "Apple",
"price": 12.50,
"image": "some url",
"description": "Some text",
"tags": [ "fruit", "red" ],
"category": "fruit"
},
]}
I created a model:
public class Product
{
public int ID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Image { get; set; }
public string Description { get; set; }
public List<Tags> Tags { get; set; }
public string Category { get; set; }
}
public class Tags
{
public string Tag { get; set; }
}
And my controller looks like:
[ApiController]
[Route("api/[controller]")]
public class ProductController : Controller
{
[HttpGet]
public IEnumerable<Product> Get()
{
StreamReader r = new StreamReader("./items.json");
string jsonString = r.ReadToEnd();
List<Product> productsList = JsonConvert.DeserializeObject<List<Product>>(jsonString);
return productsList;
}
}
The error I get:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Kata.Product]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
As I am new to c#, I am having trouble to find out how to simply return what I have in my Json file. At some point I was able to return the list by changing the json file(not the greatest idea) but all the items were null.
Thank you

You have a bug in your json classes. You can't use a special class for the tags, try to replace it by string array or list. The same about items. You deserialized the whole Root object, items just a property of this object. So since you don't need the whole object but just list of items, you have to select only them.
var jsonDeserialized= JsonConvert.DeserializeObject<Root> (jsonString);
List<Product> productsList = jsonDeserialized.Items ;
classes
public class Product
{
public int id { get; set; }
public string name { get; set; }
public double price { get; set; }
public string image { get; set; }
public string description { get; set; }
public List<string> tags { get; set; }
public string category { get; set; }
}
public class Root
{
public List<Product> Items { get; set; }
}
and fix json
{
"Items": [{
"id": 1,
"name": "Apple",
"price": 12.50,
"image": "some url",
"description": "Some text",
"tags": ["fruit", "red"],
"category": "fruit"
}]
}

Related

How to send JSON into a WEBAPI that uses a model as inputParam from the HTTPPOST

I have defined a model for my input parameter in my HTTP POST method like this
public ActionResult<JObject> Post([FromBody] APIInputObject apiInputObject)
The Model looks like this
public class APIInputObject
{
public string ApiKey { get; set; }
public Brand Brand { get; set; }
public string Query { get; set; }
public string Locale { get; set; } = "SE";
public string UseFormatter { get; set; }
}
The Brand part is further defined like this
public class Consumer
{
public string ConsumerName { get; set; }
public List<Brand> Brands { get; set; }
}
public class Brand
{
public string BrandName { get; set; }
}
The problem is now that when I send JSON is that looks like below I get an error
{
"apiKey": "xxx",
"Brand": "xx",
"query": "showBrand"
}
The error is as follows
{
"errors": {
"Brand": [
"Error converting value \"xx\" to type 'Pim.Models.Brand'. Path 'Brand', line 3, position 17."
]
},
What can I do to fix this error?
Your original JSON formatting is wrong it should be in the following format:
{
"apiKey": "xxx",
"Brand": {
"BrandName": "xx"
},
"query": "showBrand"
}
Bonus help, for your consumer object your json format would be like so:
{
"ConsumerName": "xxx",
"Brands": [{
"BrandName": "xx1"
},
{
"BrandName": "xx2"
}]
}

How to convert list of raw data to list of Json?

I have the following classes :
public class Solution
{
public string Id { get; set; }
public string ProjectName { get; set; }
public string CodeName { get; set; }
public string Description { get; set; }
public string Author { get; set; }
public string Createdate { get; set; }
public string LastEditedBy { get; set; }
public string Status { get; set; }
public string GoLive { get; set; }
public virtual List<State> States { get; set; }
}
public class State
{
public Solution Solution { get; set; }
public string SolutionId { get; set; }
public string StateId { get; set; }
public string StateValue { get; set; }
}
Where StateValue is raw json string.
When I want to deserialise this I get a json string with \r\n .
Ho to tell the json serializer to not escape that string and treat it as json because it is already json.
I want an output like this :
[
{
"id": "43c7f6d5-61dc-4c1c-8c76-e13878b7483f",
"projectName": "Test Request 2",
"codeName": "",
"description": "",
"author": "",
"createdate": "02/13/2019",
"lastEditedBy": "",
"status": "Pending",
"goLive": "02/13/2019",
"states": [
{
"id": "cd7363f8-752b-4eb2-aaa2-ef94d7685153",
"label": "Empty State",
"layerone": [
{
"StorageCloudPhysical_Custom3": "cc1",
"StorageCloudPhysical_WorkSpace": "ws for asset 2"
},
{
"StorageCloudPhysical_Custom3": "cc3",
"StorageCloudPhysical_WorkSpace": "ws for asset 4"
}
}
]
}
]
States in the json schema is the value of the operation Solution.States.Select(s => s.StateValue), which is something like List.
How can I achieve this please and thanks in advance.
EDITED:
What is your project type? Is it C# MVC?
First of all, you need to install the NewtonSoft library using Nuget, then, instantiate it on your controller.
Newtonsoft
using Newtonsoft.Json;
Now, you just "convert" your list, to a JsonList using serialize to do that.
var list = JsonConvert.SerializeObject<List<string>>(YorRawList).ToList();

What is the correct class structure in C# to convert this Json into?

I have the following Json below coming from a Rest service and I am trying to deserialize it into a C# object using this code:
var _deserializer = new JsonDeserializer();
var results = _deserializer.Deserialize<Report>(restResponse);
The deserialize method keeps returning null which tells me that my C# object is not structured correctly.
Below is the Json and my latest attempt at the C# definition.
{
"Report": [
{
"ID": "0000014",
"Age": "45",
"Details": [
{
"Status": "Approved",
"Name": "Joe"
},
{
"Status": "Approved",
"Name": "Bill"
},
{
"Status": "Submitted",
"Name": "Scott"
}
]
},
{
"ID": "10190476",
"Age": "40",
"Details": [
{
"Status": "Approved",
"Name": "Scott"
}
]
},
{
"ID": "10217480",
"Age": "40",
"Details": [
{
"Status": "Approved",
"Name": "Scott"
}
]
}
]
}
Here is my C# object:
public class Report
{
public List<WorkItem> Item= new List<WorkItem>();
}
public class WorkItem
{
public string ID { get; set; }
public int Age { get; set; }
public List<Details> Details { get; set; }
}
public class Details
{
public string Status { get; set; }
public string Name { get; set; }
}
Can someone advise what is wrong with my C# object definition to make this json deserialize correctly?
I would recommend using Json2Csharp.com to generate the classes.
public class Detail
{
public string Status { get; set; }
public string Name { get; set; }
}
public class Report
{
public string ID { get; set; }
public string Age { get; set; }
public List<Detail> Details { get; set; }
}
public class RootObject
{
public List<Report> Report { get; set; }
}
Try changing the Report class like so (The class name can be anything, the property must be Report)
public class WorkReport
{
public List<WorkItem> Report;
}
It should be trying to deserialize at the root into a class with an array/list of of workitem objects called Report.
You can try something like this. I have changed List to Dictionary You don't have a class defined at the root level. The class structure needs to match the entire JSON, you can't just deserialize from the middle. Whenever you have an object whose keys can change, you need to use a Dictionary. A regular class won't work for that; neither will a List.
public class RootObject
{
[JsonProperty("Report")]
public Report Reports { get; set; }
}
public class Report
{
[JsonProperty("Report")]
public Dictionary<WorkItem> Item;
}
public class WorkItem
{
[JsonProperty("ID")]
public string ID { get; set; }
[JsonProperty("Age")]
public int Age { get; set; }
[JsonProperty("Details")]
public Dictionary<Details> Details { get; set; }
}
public class Details
{
[JsonProperty("Status")]
public string Status { get; set; }
[JsonProperty("Name")]
public string Name { get; set; }
}
Then, deserialize like this:
Report results = _deserializer.Deserialize<Report>(restResponse);

How to display imagese which are inside the json array in windows phone7/8? which include async Task in a list box

In windows Phone application i know how to display images forma json data..
Regarding that i followed many articles on parsing the json data..
on the every article they have images in this way
"images" : "http://thegraphicsfairy.com/wp-content/uploads/2013/03/Stock-Image-Bird-Branch-GraphicsFairy1.jpg"
But i have Json Data like
"images" : [
"http://thegraphicsfairy.com/wp-content/uploads/2013/03/Stock-Image-Bird-Branch-GraphicsFairy1.jpg"
]
like that i have many images in my JSON data
"flick" : ["http://thegraphicsfairy.com/wp-content/uploads/2013/05/House-Painter-Man-Image-GraphicsFairy1.jpg"
]
"title" : ["http://scm-l3.technorati.com/11/11/17/56749/google-docs-revision.jpg%3Ft%3D20111117074048"
]
So am unable to Parse them i tried with {get,set}
and i followed many articles like
1. Facebook graph.
2. Facebook Feed.
3. Josn list box
4. Nokia Flicker
I want to display them in a Async List Box view
and one more thing i stopped at starting Because in every json data it starts with a JSON Object,.. But MY json data srats with JSON Array inside JSON OBJecet.. aganin Repeat...
This is my JSON data.. and i am getting this data from a server so i may not change the Server
{
"rC": "success",
"SpData": {
"results": [
{
"ndetails": [
{
"laoffers": [],
"offers_count": 0,
"sku": "3246",
"url": "http://www.google.com"
},
{
"laoffers": [
{
"id": "0c4ggUUkY8",
"price": "313.56",
"currency": "USD"
},
{
"id": "5OhvKwkQ",
"price": "311.95",
"currency": "USD"
}
],
"offers_count": 2,
"name": "abc.com",
"url": "http://www.bing.com"
},
{
"laoffers": [
{
"id": "0bZw4cCK",
"price": "339.99",
"currency": "USD"
},
{
"id": "CwEEA",
"shipping": "8.17",
"price": "299.99",
"currency": "USD"
}
],
"o_count": 2,
"name": "aaaa.com",
"recentoffers_count": 1,
"sku": "8567757",
"url": "http://www.avcd.com"
}
],
"model": "818BLK",
"weight": "394625.36",
"price_currency": "USD",
"gtins": [
"044558"
],
"cat_id": "23042",
"features": {
"Condition": "New",
"RAM - Memory": "2 GB - DDR3L SDRAM",
"Rear-facing Camera - Camera": "5 Megapixel"
},
"length": "215.90",
"created_at": 132657,
"variation_id": "2SKEFyM",
"geo": [
"usa"
],
"width": "130.05",
"upc": "84884848",
"ean": "47444",
"category": "Tablets",
"price": "334.99",
"updated_at": 1391388552,
"color": "Black",
"manufacturer": "abc ,amifracture",
"images_total": 1,
"images": [
"http://2.bp.blogspot.com/-AwTiVBnB_e4/T7-Wf4Z9HqI/AAAAAAAAACA/DA2UuBQsQ9g/s1600/funny-animal-pic-images-photos-02.jpg"
],
"brand": "ccc",
"sem3_id": "0iVcESQOy",
"offers_total": 169
}
],
"total_results_count": 1,
"results_count": 1,
"code": "OK",
"offset": 0
}
}
While Parsing at starting i failed Because.. in graph.facebook.com
thy have only two Json arrays..
But i have Json array Inside the Json object or JOSN object inside the json arrya..
and all the images are inside the json array which is inside the results which is inside a empty json object, and that is inside a JSOn array..
i am unable to parse some fields at starting.. and images are inside the json array are not displaying...
So i want to Parse JSON plus images By any json parsing methoid in C#(windows phone)..
and i want to deserilize the data including Images..
like this for example..
private void jsonButton_Click(object sender, RoutedEventArgs e)
{
string fbURL = "http://graph.facebook.com/microsoft";
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted += (sndr, eArgs) =>
{
if (!string.IsNullOrEmpty(eArgs.Result))
{
var json = JsonConvert.DeserializeObject<MyPageObject>(eArgs.Result);
this.jAbout.Text = json.about;
this.jCategory.Text = json.category_list[0].name;
this.jFounded.Text = json.founded;
this.jWebsite.Content = json.website;
this.jWebsite.NavigateUri = new Uri(json.website, UriKind.Absolute);
this.jAddress.Text = json.location.street + ", " + json.location.city + ", " + json.location.state;
this.jLikes.Text = json.likes.ToString();
// download cover image
string imageSource = json.cover.source;
this.CoverImage.Source = new BitmapImage(new Uri(imageSource, UriKind.Absolute));
}
};
webClient.DownloadStringAsync(new Uri(fbURL, UriKind.Absolute));
}
At here they have image as normal way.. But i have image inside a JSON array..
and They used Root Object at starting Because they have only one Main JSON arrya.. But i have a many Json arrays and Objects which means [{ or { some data [{ some data... Like that i have..
And at here i want the out put exact like Nokia flicker.... But over there They parsed XML json data.. But i want ONly for JSON data..
Here is a link that convert a json string to respective c# class architecture. Below is the class architecture of your json string.
public class Ndetail
{
public List<object> laoffers { get; set; }
public int offers_count { get; set; }
public string sku { get; set; }
public string url { get; set; }
public string name { get; set; }
public int? o_count { get; set; }
public int? recentoffers_count { get; set; }
}
public class Features
{
public string Condition { get; set; }
public string __invalid_name__RAM - Memory { get; set; }
public string __invalid_name__Rear-facing Camera - Camera { get; set; }
}
public class Result
{
public List<Ndetail> ndetails { get; set; }
public string model { get; set; }
public string weight { get; set; }
public string price_currency { get; set; }
public List<string> gtins { get; set; }
public string cat_id { get; set; }
public Features features { get; set; }
public string length { get; set; }
public int created_at { get; set; }
public string variation_id { get; set; }
public List<string> geo { get; set; }
public string width { get; set; }
public string upc { get; set; }
public string ean { get; set; }
public string category { get; set; }
public string price { get; set; }
public int updated_at { get; set; }
public string color { get; set; }
public string manufacturer { get; set; }
public int images_total { get; set; }
public List<string> images { get; set; }
public string brand { get; set; }
public string sem3_id { get; set; }
public int offers_total { get; set; }
}
public class SpData
{
public List<Result> results { get; set; }
public int total_results_count { get; set; }
public int results_count { get; set; }
public string code { get; set; }
public int offset { get; set; }
}
public class RootObject
{
public string rC { get; set; }
public SpData SpData { get; set; }
}
DataContractJsonSerializer useful with this class architecture to parse a json string.
Below is the sample code.
// json parsing code.
MemoryStream memoryStream = new MemoryStream(Encoding.Unicode.GetBytes(Your json string));
DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(RootObject));
RootObject parsedData= dataContractJsonSerializer.ReadObject(memoryStream) as RootObject;
if(parsedData!=null)
{
MessageBox.Show(parsedData.SpData.results.First().ndetails.First().url.ToString());
}

JSON Deserialization Error in C#

I want to Deserialize a JSON object to C# but I'm getting this exception:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'FYP___Task_1.RootObject' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.
I've tried to get this error solved by different solutions I found on StackOverflow but no one worked.
The JSON I'm using is as follows:
[
{
"rating_count": 158271,
"genres": [
"Action",
"Crime",
"Thriller"
],
"rated": "PG-13",
"language": [
"English",
"French",
"Mandarin"
],
"rating": 6.7,
"country": [
"France",
"USA"
],
"release_date": 20021011,
"title": "Transporter\n \"The Transporter\"",
"year": 2002,
"filming_locations": "Avenue de Saissy, Cannes, Alpes-Maritimes, France",
"imdb_id": "tt0293662",
"directors": [
"Corey Yuen"
],
"writers": [
"Luc Besson",
"Robert Mark Kamen"
],
"actors": [
"Jason Statham",
"Qi Shu",
"Matt Schulze",
"François Berléand",
"Ric Young",
"Doug Rand",
"Didier Saint Melin",
"Tonio Descanvelle",
"Laurent Desponds",
"Matthieu Albertini",
"Vincent Nemeth",
"Jean-Yves Bilien",
"Jean-Marie Paris",
"Adrian Dearnell",
"Alfred Lot"
],
"also_known_as": [
"Transporter"
],
"poster": {
"imdb": "http://ia.media-imdb.com/images/M/MV5BMTk2NDc2MDAxN15BMl5BanBnXkFtZTYwNDc1NDY2._V1_SY317_CR3,0,214,317_.jpg",
"cover": "http://imdb-poster.b0.upaiyun.com/000/293/662.jpg!cover?_upt=cd37cf0e1385015165"
},
"runtime": [
"92 min"
],
"type": "M",
"imdb_url": "http://www.imdb.com/title/tt0293662/"
}
]
The classes I'm using:
public class Poster
{
public string imdb { get; set; }
public string cover { get; set; }
}
public class RootObject
{
public int rating_count { get; set; }
public List<string> genres { get; set; }
public string rated { get; set; }
public List<string> language { get; set; }
public double rating { get; set; }
public List<string> country { get; set; }
public int release_date { get; set; }
public string title { get; set; }
public int year { get; set; }
public string filming_locations { get; set; }
public string imdb_id { get; set; }
public List<string> directors { get; set; }
public List<string> writers { get; set; }
public List<string> actors { get; set; }
public List<string> also_known_as { get; set; }
public Poster poster { get; set; }
public List<string> runtime { get; set; }
public string type { get; set; }
public string imdb_url { get; set; }
}
Your JSON object has the structure [ {..} ] which means it is a list of objects. In your case, your list has only one object, but it is still a list. What you are trying to do is turn the list into an object, so you get an exception.
The solution is either to change your JSON to {..} (i.e. remove the square brackets) OR deserialize the JSON into an array of RootObject and then just read the first one, for example:
RootObject[] myArray = json.Deserialize<RootObject[]>("json goes here");
RootObject firstObject = myArray[0];
We can try the below option also.
var root = response1.Content.ReadAsAsync<RootObject>().Result;
GridView1.DataSource = root.items;

Categories

Resources