Array JSON deserialize - c#

I'm trying to get the data from a website RSS converting it to JSON. I got this JSON string:
http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http%3A%2F%2Frss.tecmundo.com.br%2Ffeed
I'm using lists to get the values but I got this error "Cannot create an instance of the abstract class or interface" and I don't know how to solve it. It happens in this line.
IList<News> content = new IList<News>();
Here is my code.
public class News
{
public string author { get; set; }
public string title { get; set; }
public string content { get; set; }
public string contentSnippet { get; set; }
public string link { get; set; }
public string publishedDate { get; set; }
public string[] getFeed(string Website)
{
string path = #"http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=" + Website;
var json = new WebClient().DownloadString(path);
JObject jsonObject = JObject.Parse((string)json);
IList<JToken> jsonData = jsonObject["responseData"]["feed"]["entries"]["0"].Children().ToList();
IList<News> content = new IList<News>();
foreach(JToken data in jsonData)
{
News finalData1 = JsonConvert.DeserializeObject<News>(jsonData.ToString());
content.Add(finalData1);
}
return new string[] { "I must return something here." };
}
}
Here is the tool I'm using to visualize better the JSON string: http://jsonschema.net/#/

The error you're getting has nothing to do with JSON. It is because you're trying to create an instance of an interface. You could just fix that by giving it the concrete List<T> class:
IList<News> content = new List<News>();
However, the simpler way of converting the IList<JToken> to an IList<News> is probably to use LINQ again - you can do all of this in one step pretty easily:
IList<News> content = jsonObject["responseData"]["feed"]["entries"]["0"]
.Children()
.Select(token => JsonConvert.DeserializeObject<News>(token.ToString())
.ToList();
That compiles, but isn't actually want due to the data you've got. entries is an array, so you probably want:
JArray array = (JArray) jsonObject["responseData"]["feed"]["entries"];
var content = array
.Select(token => JsonConvert.DeserializeObject<News>(token.ToString())
.ToList();

Your problem has nothing to do with the json, but with trying to create an instance of an interface which is not possible in c#. You need to create an instance of a concrete class that implements the IList interface. List would be one example. There are others, including arrays.

Related

Deserialize json string into a list of .NET objects

Requirement
I am trying to build a function that takes a json string as input. and outputs list of object.
The json string is in a similar format to this:
{\"custlist\":[{\"cust_name\":\"Vincent\",\"cust_id\":\"klq:206f387:2d08m92t6\"},{\"cust_name\":\"Joyce\",\"cust_id\":\"125g:1474grx:2d03t9dld\"}]}
My Search
There are plenty of solutions deserialize json array to list of objects, but the array starts at the beginning of the string. i.e. without the \"custlist\": part
If we have \"custlist\": part in the json string, those solutions break.
My Code
Here is my code in C#. It is working, but I had to use regular expression to match the input string. Seems over-complicated. There must be an easier way. Anyone knows, please kindly advise
public void Test()
{
string str = {\"custlist\":[{\"cust_name\":\"Vincent\",\"cust_id\":\"klq:206f387:2d08m92t6\"},{\"cust_name\":\"Joyce\",\"cust_id\":\"125g:1474grx:2d03t9dld\"}]};
List<Customer> list = Json2List<Customer>(str);
foreach (Customer c in list)
{
console.writeline ("name=" + c.cust_name);
console.writeline ("id=" + c.cust_id);
}
}
public List<T> Json2List<T>(string s)
{
string json_listname = Regex.Match(s, "\"" + #"(\w+?)" + "\":").Groups[0].Value;
JObject jObject = JObject.Parse(s);
List<JToken> jTokenList = jObject.GetValue(json_listname).ToList();
List<T> LstT = new List<T>();
foreach (JToken jt in jTokenList)
{
T obj = jt.ToObject<T>();
LstT.Add(obj);
}
return LstT;
}
public class Customer
{
public string cust_name { get; set; }
public string cust_id { get; set; }
}
I am really lost as to what the problem is, but essentially:
public class CustomerList {
[JsonProperty("custlist")]
public Customer[] Customers { get; set; }
}
public class Customer
{
[JsonProperty("cust_name")]
public string Name { get; set; }
[JsonProperty("cust_id")]
public string Id { get; set; }
}
var sample = "{\"custlist\":[{\"cust_name\":\"Vincent\"},{\"cust_id\":\"klq206f3872d08m92t6\"},{\"cust_name\":\"Joyce\"},{\"cust_id\":\"125g1474grx2d03t9dld\"}]}";
var result = JsonConvert.DeserializeObject<CustomerList>(sample).Customers;
// Or!
var dictResult = JsonConvert.DeserializeObject<Dictionary<string, Customer[]>>(sample)["custlist"];
Looks like it's a JSON object that is stored in a JSON string.
So deserialize it as a string first, then as a list of the correct type. You can do both using JsonConvert.DeserializeObject:
Update: I just realized that the array in the JSON is a property of the JSON object, which I didn't account for here. I don't have time to fix that right now, but I hope you get the idea.
public List<T> Json2List<T>(string s)
{
string json_object = JsonConvert.DeserializeObject<string>(s);
return JsonConvert.DeserializeObject<List<T>>(json_object);
}
But I would also look into why the data you're getting is double-serialized like that.

How would I deserialize a JSON String when i dont require every value?

Scenario
I am creating a small stateless API type thing which pulls information from a single API, takes only the relevant data, Takes the data and uses it as a search term within the second API. From here I then want to take only relevant information and return it to the user.
The Issue
The JSON is returned with many values within a single array(most of which are not required) and from my understanding, I need to deserialize the string into individual objects so I can then do a .Count to find out the number of cards which have been returned.
JSON String
The JSON String is large so I will just leave this link in case anyone is interested.
https://api.pokemontcg.io/v1/cards?name=POKEMONNAME
Values I am looking for
Name
SpriteURL
Types
Artist
What I have already Tried
var obj = JsonConvert.DeserializeObject<PokemonTCGApi>(data);
List<PokemonTCGApi> obj = JsonConvert.DeserializeObject<List<PokemonTCGApi>>(data);
PokemonTCGApi[] objList = new JavaScriptSerializer().Deserialize<Order[]>(orderJson);
var obj = JsonConvert.DeserializeObject(data);
List<JSONClass.Card> list = JsonConvert.DeserializeObject<List<JSONClass.Card>>(data);
PokeTCG Model
public class PokemonTCGApi
{
public string cardName { get; set; }
public string imageUrl { get; set; }
public string Types { get; set; }
public string Artist { get; set; }
public PokemonTCGApi(string cardName, string imageUrl, string types, string artist)
{
this.cardName = cardName;
this.imageUrl = imageUrl;
Types = types;
Artist = artist;
}
public PokemonTCGApi(string cardName)
{
this.cardName = cardName;
}
}
If you don't require every value from json string then create to class object what ever response you needed from deserialize, and do like that.
var data = JsonConvert.DeserializeObject<List<EmployeeViewModel>>(json_array_string);
var data1 = JsonConvert.DeserializeObject<EmployeeViewModel>(json_string);
You can deserialize to a List and take only the relevant bits from there.
var myData = JsonConvert.DeserializeObject<List<ExpandoObject>>(jsonString);

Deserialising json string to List<T>

I have a web service that is outputting JSON in the form
{"AppointmentList":[{"AppointmentList":{"id":"1","MeetingId":"1","MeetingName":"Test Meeting 1","Length":"90","Room":"B2C","DateTimeFrom":"1st Sept 2016","Venue":"The old pub","DateCreated":"2016-08-30 00:00:00","DateDue":"2016-09-01 00:00:00","UserId":"JohnsonPa"}},{"AppointmentList":{"id":"2","MeetingId":"2","MeetingName":"Test Meeting 2","Length":"60","Room":"B2C","DateTimeFrom":"11th Sept 2016","Venue":"The old pub","DateCreated":"2016-09-01 00:00:00","DateDue":"2016-09-12 00:00:00","UserId":"JohnsonPa"}...}]}
I am trying to deserialise this in to List. Normally, I would have a Base Class that would contain a property List AppointmentList {get; set;}, however, that would mean that I can't use type T and need a pile of duplicate code for each class.
I can certainly create BaseClass with a property public List Data {get; set;} however, as the JSON won't deserialise to Data (incorrect name) and the JSON PropertyName can't be set to the class name derived from typeof(T).ToString().
Is there a way to achieve what I'm trying to do without resorting to lots of code duplication?
I've tried casting the deserialiser to JArray and creating a reader from that, but this throws an exception.
Im not sure if this is exactly what you need, but maybe something like this would work? It allows you to successfully deserialize to a JArray like you state you tried at the end of your question.
JArray result = JsonConvert.DeserializeObject<dynamic>(json).AppointmentList;
Here how to convert it to List<object>
dynamic data = JsonConvert.DeserializeObject(json);
JArray array = data.AppointmentList;
List<object> objectList = array.ToObject<List<object>>();
What is wrong with generics? If you want a schemaless data structure use JObject or dynamic if not you can try this.
class Program
{
public const string json = #"{""AppointmentList"":[{""AppointmentList"":{""id"":""1"",""MeetingId"":""1"",""MeetingName"":""Test Meeting 1"",""Length"":""90"",""Room"":""B2C"",""DateTimeFrom"":""1st Sept 2016"",""Venue"":""The old pub"",""DateCreated"":""2016-08-30 00:00:00"",""DateDue"":""2016-09-01 00:00:00"",""UserId"":""JohnsonPa""}},{""AppointmentList"":{""id"":""2"",""MeetingId"":""2"",""MeetingName"":""Test Meeting 2"",""Length"":""60"",""Room"":""B2C"",""DateTimeFrom"":""11th Sept 2016"",""Venue"":""The old pub"",""DateCreated"":""2016-09-01 00:00:00"",""DateDue"":""2016-09-12 00:00:00"",""UserId"":""JohnsonPa""}}]}";
static void Main(string[] args)
{
var items = Newtonsoft.Json.JsonConvert.DeserializeObject<AppointmentItemList<Meeting1>>(json).GetList();
var items2 = Newtonsoft.Json.JsonConvert.DeserializeObject<AppointmentItemList<Meeting2>>(json).GetList();
Console.ReadLine();
}
public class AppointmentItemList<T>
{
public List<AppointmentItem> AppointmentList { get; set; }
public class AppointmentItem
{
public T AppointmentList { get; set; }
}
public IList<T> GetList()
{
return AppointmentList.Select(al => al.AppointmentList).ToList();
}
}
public class Meeting1
{
[Newtonsoft.Json.JsonProperty("id")]
public string Id { get; set; }
public string MeetingName { get; set; }
}
public class Meeting2
{
[Newtonsoft.Json.JsonProperty("id")]
public string Id { get; set; }
public string Room { get; set; }
}
}

Retrieving data from an Object(deserialized json) in C#

I have a JSON string that I am trying to parse, using C#. I have used JsonConvert to serialize my data into a JSON string.
Here is my sample JSON string:
{"names": ["John", "Joe", "Jack"], "nationality": "American"}
I am able to deserialize this string into an object using JsonConvert.DeserializeObject(x);
The problem is, I dont know how to read from the object, using C#. Can someone help me out?
public class People
{
[JsonProperty("names")]
public List<string> Names;
[JsonProperty("nationality")]
public string Nationality;
}
Other answers are technically correct, but using JsonPropertyAttribute is a universally accepted convention. Then use JsonConvert:
var people = JsonConvert.DeserializeObject<People>(x);
A better approach would be to define a class with the expected structure, then using JavaScriptSerializer to deserialize it:
class NameSet
{
public IList<string> names { get; set; }
public string nationality { get; set; }
}
var serializer = new JavaScriptSerializer();
var nameset = serializer.Deserialize<NameSet>(jsonString);
Create a custom class like this:
public class CustomData
{
public string[] Names { get; set; }
public string Nationality { get; set; }
public CustomData() { }
}
And use JsonConvert to deserialize yo an object of this type:
CustomData data = JsonConvert.DeserializeObject<CustomData>(x);
The following should suffice:
public class PeopleGroup {
public string[] names { get; set; }
public string nationality { get; set }
}
var myObject = JsonConvert.DeserializeObject<PeopleGroup>(x);
Basically, you create a strongly typed object, and deserialise directly into it.
If you don't want to actually define a class, you can use an anonymous type:
string json = "{\"names\": [\"John\", \"Joe\", \"Jack\"], \"nationality\": \"American\"}";
// Just defining the structure of the anonymous type
var x = new { names = new string[0], nationality = string.Empty };
x = JsonConvert.DeserializeAnonymousType(json, x);
You should use dataContractJsonSerializer class, it is faster and most important is it is inbuilt class of .Net Framework. I will post solution in my next commment, in that How can we use DataContractJsonSerializer class.Now I cant post solution because in my end net is too slow :(, but I will post today.

Deserialization of Json without name fields

I need to deserialize the following Json, which according to Jsonlint.com is valid, but ive not come across this before or cannot find examples of similar Json and how to deal with it?
[1,"Bellegrove / Sherwood ","76705","486","Bexleyheath Ctr",1354565507000]
My current system with like this:
Data class:
[DataContract]
public class TFLCollection
{ [DataMember(Name = "arrivals")]
public IEnumerable<TFLB> TFLB { get; set; }
}
[DataContract]
public class TFLB
{
[DataMember]
public string routeName { get; set; }
[DataMember]
public string destination { get; set; }
[DataMember]
public string estimatedWait { get; set; }
}
Deserializer:
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(TFLCollection));
using (var stream = new MemoryStream(Encoding.Unicode.GetBytes(result)))
{ var buses = (TFLCollection)serializer.ReadObject(stream);
foreach (var bus in buses.TFLBuses)
{
StopFeed _item = new StopFeed();
_item.Route = bus.routeName;
_item.Direction = bus.destination;
_item.Time = bus.estimatedWait;
listBox1.Items.Add(_item);
My exsiting deserializer works with a full Json stream and iterates through it, but in my new Json I need to deserialize, it only have 1 item, so I wont need to iterate through it.
So is it possible to deserialize my Json example using a similar method than I currently do?
I would say that you are attempting to overcomplicate things. What you have is a perfectly formed json array of strings. If I were you I would deserialize that to an .net array first, and then write a 'mapper' function to copy the values across:
public TFLB BusRouteMapper(string[] input)
{
return new TFLB {
Route = input[x],
Direction = input[y],
};
}
And so on. Of course this assumes that you know what order your json is going to be in, but if you are attempting this in the first place then you must do!

Categories

Resources