I am trying to resolve the some complex JSON I am receiving to convert it to a C# Model so I can analyse and manipulate the data. I have tried converting the JSON by using JSON to C# but it doesn't work well when the name is dynamic. I want to focus on the Data.
The Item1 and SubItem1 etc are all variable strings so I can't hard code a model to pull them and the text is just a string within. The issue I am having is how to get the JSON into a usable format to access the Item1, SubItem1 and text values to manipulate their data.
I have looked into Dictionaries as suggested elsewhere but I am having no luck.
Model I have tried
public class Data
{
public string Status { get; set; }
public ResultInfo ResultInfo { get; set; }
public string Message { get; set; }
public Dictionary<string, SubData> Data {get;set;}
}
public class SubData
{
public Dictionary<string,List<string>> item {get;set;}
}
JSON
{
"Status": "OK",
"ResultInfo": {
"Prev": "PageUPURL",
"Next": "PageDownURL",
"total_count": "37",
"per_page": "3",
"page": "1"
},
"Message": "Ok.",
"Data": {
"Item1": [
{
"SubItem1": [
"Text"
]
}
],
"Item2": [
{
"SubItem2": [
"Text",
"Text",
"Text"
]
}
],
"Item3": [
{
"SubItem3": [
"Text"
]
},
{
"SubItem4": [
"Text"
]
}
]
}
}
Any suggestions, advice or help would be gratefully received.
For those task I use https://app.quicktype.io .
Because it can easly reconnize dictionary when the property name are numeric you just need to edit your Json a little.
And past it into the tool.
{
"Status": "OK",
"ResultInfo": {
"Prev": "PageUPURL",
"Next": "PageDownURL",
"total_count": "37",
"per_page": "3",
"page": "1"
},
"Message": "Ok.",
"Data": {
"1": [
{
"1": [
"Text"
]
}
],
"2": [
{
"1": [
"Text",
"Text",
"Text"
]
}
],
"3": [
{
"1": [
"Text"
]
},
{
"2": [
"Text"
]
}
]
}
}
The result class is :
public partial class Root
{
[JsonProperty("Status")]
public string Status { get; set; }
[JsonProperty("ResultInfo")]
public ResultInfo ResultInfo { get; set; }
[JsonProperty("Message")]
public string Message { get; set; }
[JsonProperty("Data")]
public Dictionary<string, List<Dictionary<string, List<string>>>> Data { get; set; }
}
public partial class ResultInfo
{
[JsonProperty("Prev")]
public string Prev { get; set; }
[JsonProperty("Next")]
public string Next { get; set; }
[JsonProperty("total_count")]
[JsonConverter(typeof(ParseStringConverter))]
public long TotalCount { get; set; }
[JsonProperty("per_page")]
[JsonConverter(typeof(ParseStringConverter))]
public long PerPage { get; set; }
[JsonProperty("page")]
[JsonConverter(typeof(ParseStringConverter))]
public long Page { get; set; }
}
Online Demo https://dotnetfiddle.net/1j42kR
nota bene: that tool also included a converter from "string" to "long" for "per_page": "3",
Change below line
public Dictionary<string, SubData> Data {get;set;}
as
public Dictionary<string, Dictionary<string, string[]>[]> Data { get; set; }
Related
I'm trying to deserialize a response from a firebase realtime database get/json, to get user locations that are online
example response:
{
"test1": {
"id": "test-1",
"location": {
"lat": 37.33097983,
"long": -122.03063943
}
},
"test2": {
"id": "test-2",
"location": {
"lat": 37.33021864,
"long": -122.02370753
}
},
"test3": {
"id": "test-3",
"location": {
"lat": 37.32873847,
"long": -122.01980584
}
},
"test4": {
"id": "test-4",
"location": {
"lat": 37.32563464,
"long": -122.01972943
}
},
"test5": {
"id": "test-5",
"location": {
"lat": 37.32472734,
"long": -122.02127163
}
}
}
I put this into a json to c# class converter and it creates a class for each user (Test1, Test2, Test3, etc). This would work, if the users from the example are the only users to be in the response. If I had a sixth user named "6test", I would need to also create a class for that one too.
How can I use the Json converter (Newtonsoft.Json or System.Text.Json) to return a user in a list<User>
you can use a dictionary in your case
using Newtosoft.Json;
Dictionary<string,Test> tests = JsonConvert.DeserializeObject<Dictionary<string,Test>>(json);
classes
public class Location
{
public double lat { get; set; }
public double #long { get; set; }
}
public class Test
{
public string id { get; set; }
public Location location { get; set; }
}
how to use
Test test5=tests["test5"];
or if you want to use list instead of Dictionary
var jsonParsed = JObject.Parse(json);
List<ListTest> list = jsonParsed.Properties().Select(p => new ListTest { Name = p.Name, Test = p.Value.ToObject<Test>() }).ToList();
how to use
Test test4 = list.Where(f=>f.Name=="test4").Select(t=>t.Test).FirstOrDefault();
class
public class ListTest
{
public string Name { get; set; }
public Test Test { get; set; }
}
I want to deserialize this json to a List of Product objects but i get this error:
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[ShoppingList.Product]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
This is my code:
{
"records": [
{
"id": "60",
"name": "Rolex Watch",
"description": "Luxury watch.",
"price": "25000",
"category_id": "1",
"category_name": "Fashion"
},
{
"id": "48",
"name": "Bristol Shoes",
"description": "Awesome shoes.",
"price": "999",
"category_id": "5",
"category_name": "Movies"
},
{
"id": "42",
"name": "Nike Shoes for Men",
"description": "Nike Shoes",
"price": "12999",
"category_id": "3",
"category_name": "Motors"
}
]
}
public class Product
{
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
public decimal price { get; set; }
public int category_id { get; set; }
public string category_name { get; set; }
}
public async Task<List<Product>> GetProductsAsync()
{
Products = new List<Product>();
var uri = new Uri("https://hostname/api/product/read.php");
try
{
var response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
Products = JsonConvert.DeserializeObject<List<Product>>(content);
}
}
catch (Exception )
{
throw;
}
return Products;
}
Your Json is not a List<Product>, its an object with a single property called records which is a List<Product>.
So your actual C# model is this:
public class RootObject
{
public List<Product> records { get; set; }
}
And you deserialize like this:
RootObject productsRoot = JsonConvert.DeserializeObject<RootObject>(content);
The issue is that your response JSON you've provided is an object which contains a list, but you are trying to deserialize the data into a straight list. I think all you need to do is add a second class which contains the list into your code, then deserialize using that list.
public class Product
{
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
public decimal price { get; set; }
public int category_id { get; set; }
public string category_name { get; set; }
}
public class RootProduct
{
public List<Product> Products {get;set;}
}
Then you change your deserialization portion from List to
RootProduct response = JsonConvert.DeserializeObject<RootProduct>(content);
The usage would then simply be products. response.Products to access the list of items.
The problem is your json structure. Below is an example of a json array - notice i removed the property "records".
[
{
"id": "60",
"name": "Rolex Watch",
"description": "Luxury watch.",
"price": "25000",
"category_id": "1",
"category_name": "Fashion"
},
{
"id": "48",
"name": "Bristol Shoes",
"description": "Awesome shoes.",
"price": "999",
"category_id": "5",
"category_name": "Movies"
},
{
"id": "42",
"name": "Nike Shoes for Men",
"description": "Nike Shoes",
"price": "12999",
"category_id": "3",
"category_name": "Motors"
}
]
Given this data
[
{
"id": "60",
"name": "Rolex Watch",
"description": "Luxury watch.",
"price": "25000",
"category_id": "1",
"category_name": "Fashion"
},
{
"id": "48",
"name": "Bristol Shoes",
"description": "Awesome shoes.",
"price": "999",
"category_id": "5",
"category_name": "Movies"
},
{
"id": "42",
"name": "Nike Shoes for Men",
"description": "Nike Shoes",
"price": "12999",
"category_id": "3",
"category_name": "Motors"
}
]
And this class definition
public class Order
{
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
public string price { get; set; }
public string category_id { get; set; }
public string category_name { get; set; }
}
The code to convert it from JSON to a C# List<Order> is:
public static List<Order> Map(string json)
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<List<Order>>(json);
}
I want to access variables values from json list in C#.
my json String is as below
plase suggest me the way to access variable from json list .
example :-
{
"BillTransList": [
{
"aa": "13",
"ss": "200",
"LessItemList": [
{
"a": "13",
"b": "19"
},
{
"a": "17",
"b": "18"
}
]
},
{
"aa": "13",
"ss": "200",
"LessItemList": [
{
"a": "3",
"b": "9"
},
{
"a": "7",
"b": "8"
}
]
}
],
"aq": "2"
}
suggestions and corrections are welcome
thanks
You can create class to deserialize your JSON.
public class LessItemList
{
public string a { get; set; }
public string b { get; set; }
}
public class BillTransList
{
public string aa { get; set; }
public string ss { get; set; }
public List<LessItemList> LessItemList { get; set; }
}
public class RootObject
{
public List<BillTransList> BillTransList { get; set; }
public string aq { get; set; }
}
than you can deserialize using the below code
var jsonResponse = JsonConvert.DeserializeObject<RootObject>(result);
than you can access LessItemList simply by using LINQ.
Aliasgar Rajpiplawala's answer is Nice.
And I will do some supplement:
You should use some library to deserialize JSON string, such like Json.NET (Newtonsoft JSON).
And you can create your class using Json2Csharp (http://json2csharp.com/). Paste your json into it, and your class will be generated automic.
I have a json data when i'm trying to parse it returns error incorrect syntax please help me found the syntax error.
[{"isData":"Yes","Details":"[{"Id":"70","Name":"Test","FileName":"Uploaded","FileFormat":".mp4","FileType":"Video","FileDuration":"00:30:00 ","StartTime":"/Date(1372617000000)/","EndTime":"/Date(1372681771000)/","File":"2562013172331815635077778118152815.mp4"}]"}]
And this is the class that is used to serialize data, i am using javascript serializer
public enum Data
{
Yes,
No
}
public class MessageResponse()
{
public string isData { get; set; }
public string Details { get; set; }
}
List<MessageResponse> response = new List<MessageResponse>();
string strJson="[{"Id":"70","Name":"Test","FileName":"Uploaded","FileFormat":".mp4","FileType":"Video","FileDuration":"00:30:00 ","StartTime":"/Date(1372617000000)/","EndTime":"/Date(1372681771000)/","File":"2562013172331815635077778118152815.mp4"}]";
var newData = new MessageResponse
{
isData = Data.Yes.ToString(),
Details = strJson
};
response.Add(newData);
var jsonSerialiser1 = new JavaScriptSerializer();
string result = jsonSerialiser1.Serialize(response);
That's invalid JSON. The Details property is incorrectly formatted. You should remove the quotes around the value. It should be like this:
[
{
"isData": "Yes",
"Details": [
{
"Id": "70",
"Name": "Test",
"FileName": "Uploaded",
"FileFormat": ".mp4",
"FileType": "Video",
"FileDuration": "00:30:00 ",
"StartTime": "/Date(1372617000000)/",
"EndTime": "/Date(1372681771000)/",
"File": "2562013172331815635077778118152815.mp4"
}
]
}
]
or if you want Details to be a string property (representing JSON), which is kinda lame, you should properly escape the double quotes:
[
{
"isData": "Yes",
"Details": "[{\"Id\":\"70\",\"Name\":\"Test\",\"FileName\":\"Uploaded\",\"FileFormat\":\".mp4\",\"FileType\":\"Video\",\"FileDuration\":\"00: 30: 00\",\"StartTime\":\"/Date(1372617000000)/\",\"EndTime\":\"/Date(1372681771000)/\",\"File\":\"2562013172331815635077778118152815.mp4\"}]"
}
]
This structure you will be able to map to your current object model. But I would recommend you using the first approach.
Remove the " from the details data:
[{
"isData":"Yes",
"Details":
[{
"Id":"70",
"Name":"Test",
"FileName":"Uploaded",
"FileFormat":".mp4",
"FileType":"Video",
"FileDuration":"00:30:00",
"StartTime":"/Date(1372617000000)/",
"EndTime":"/Date(1372681771000)/",
"File":"2562013172331815635077778118152815.mp4"
}]
}]
Details should be of type class (i.e. user defined class) and it should hold all the properties.
public class Details
{ public int Id {get; set;} ... }
Firstly your json is invalid.
It should not have the " before and after the [ ]
[
{
"isData": "Yes",
"Details": [
{
"Id": "70",
"Name": "Test",
"FileName": "Uploaded",
"FileFormat": ".mp4",
"FileType": "Video",
"FileDuration": "00: 30: 00",
"StartTime": "/Date(1372617000000)/",
"EndTime": "/Date(1372681771000)/",
"File": "2562013172331815635077778118152815.mp4"
}
]
}
]
Secondly, your class could be improved to be:
public class MessageResponse
{
public string isData { get; set; }
public Details Details { get; set; }
}
public class Details
{
public int Id { get; set; }
public string Name { get; set; }
public string FileName { get; set; }
public string FileFormat { get; set; }
public string FileType { get; set; }
public string FileDuration { get; set; }
public string StartTime { get; set; }
public string EndTime { get; set; }
public string File { get; set; }
}
You would probably want to set up the correct data types though for things like Start Time etc...
I'm trying to parse out some information I've retrieved from RottenTomatoes that's in a JSON format
{
"cast": [
{
"id": "162655641",
"name": "Tom Hanks",
"characters": [
"Woody"
]
},
{
"id": "162655909",
"name": "Tim Allen",
"characters": [
"Buzz Lightyear"
]
},
{
"id": "162655020",
"name": "Joan Cusack",
"characters": [
"Jessie the Cowgirl"
]
},
{
"id": "162672460",
"name": "Ned Beatty",
"characters": [
"Lots-o'-Huggin' Bear",
"Lotso"
]
},
{
"id": "162657445",
"name": "Richard Kind",
"characters": [
"Bookworm"
]
},
{
"id": "162654813",
"name": "Erik von Detten",
"characters": [
"Sid"
]
},
{
"id": "770713272",
"name": "James Anthony Cotton",
"characters": []
}
],
"links": {
"rel": "http://api.rottentomatoes.com/api/public/v1.0/movies/770672122.json"
}
}
I'm just trying to get this code to work but I'm getting an InvalidOperationException and this error
"Type 'System.String' is not supported for deserialization of an array."
Here's my code in main
string json = File.ReadAllText("json.txt");
CastInfo castMember = new JavaScriptSerializer().Deserialize<CastInfo>(json);
Here are my classes
public class CastInfo
{
public List<CustomCastInfo> cast { get; set; }
}
public class CustomCastInfo
{
public string id { get; set; }
public string name { get; set; }
public List<string> characters { get; set; }
}
And advice? And I realize I need to do something about the "links" in the bottom, but even when I delete that it still doesn't work.
I just tried running this with the json you provided and it worked fine.
using System.Collections.Generic;
using System.IO;
using System.Web.Script.Serialization;
namespace JsonDeserialization
{
class Program
{
static void Main(string[] args)
{
string json = File.ReadAllText("json.txt");
CastInfo castMember = new JavaScriptSerializer().Deserialize<CastInfo>(json);
}
}
public class CastInfo
{
public List<CustomCastInfo> cast { get; set; }
}
public class CustomCastInfo
{
public string id { get; set; }
public string name { get; set; }
public List<string> characters { get; set; }
}
}