Deserialize json string into a list of .NET objects - c#

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.

Related

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);

Querying JSON using SelectTokens? With Newtonsoft.Json.Linq in C#

I'm trying to make use of Netwonsoft.JSON.Linq in C#, to change the "statusCode" values in the following JSON:
{
"disbursements":[
{
"id":"1f337641",
"contactId":"f5eb2",
"statusCode":166000005,
"amount":8,
"category":166000001
},
{
"id":"027a4762",
"contactId":"f5eb2038",
"statusCode":166000000,
"amount":4000,
"category":166000000
}
]
}
So, inside the JSON data is: "disbursements" which is JSON array. I have to change the "statusCode" of each item in the array to 166000005. I'm able to retrieve statusCode of the first one using
JObject jsonText = JObject.Parse(bodyText);
var statusCode = (int)jsonText.SelectToken("disbursements[0].statusCode");
But I need a solution with loop or LINQ that changes all the values, not just the first.
The following code sets or adds "statusCode": 166000005 to every entry in the disbursement array:
var jsonText = JObject.Parse(bodyText);
foreach (var disbursement in jsonText.SelectTokens("disbursements[*]"))
{
disbursement["statusCode"] = 166000005;
}
Notes:
The query string "disbursements[*]" contains the JSONPath wildcard operator [*]. This operator matches all array elements under the parent element "disbursement".
Json.NET supports JSONPath syntax as documented in Querying JSON with JSONPath.
SelectTokens() is used rather than SelectToken() to loop through multiple possible matches.
The JToken item setter disbursement["statusCode"] = 166000005 will replace the "statusCode" property if present and add it if not.
A simple, atomic value such as 166000005 can be set directly into a JToken hierarchy. For a complex POCO you would need to call JToken.FromObject() to serialize it to a JToken before setting it in the hierarchy, e.g.:
disbursement["statusCode"] =
JToken.FromObject( new { oldValue = disbursement["statusCode"], newValue = 166000005 } );
Sample working .Net fiddle.
I would create classes to represent the data. Here is my solution:
Create the data holder classes:
public class Disbursement
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("contactId")]
public string ContactId { get; set; }
[JsonProperty("statusCode")]
public int StatusCode { get; set; }
[JsonProperty("amount")]
public int Amount { get; set; }
[JsonProperty("category")]
public int Category { get; set; }
}
The collection:
public class Disbursements
{
[JsonProperty("disbursements")]
public List<Disbursement> Items { get; set; } = new List<Disbursement>();
}
And then the loading / modifying / saving data:
class Program
{
static void Main(string[] args)
{
var disbursements =
JsonConvert.DeserializeObject<Disbursements>(
File.ReadAllText(
"data.json",
Encoding.UTF8
)
);
foreach (var disbursement in disbursements.Items)
{
disbursement.StatusCode = 166000005;
}
string modifiedContent = JsonConvert.SerializeObject(disbursements);
File.WriteAllText(
"modifiedData.json",
modifiedContent,
Encoding.UTF8
);
}
}

Json Deserialize a webclient response C#

I am new in C# and I know there are hundreds of examples on the google for Json deserialization. I tried many but could not understand how C# works for deserialization.
using (var client = new WebClient())
{
client.Headers.Add("Content-Type", "text/json");
result = client.UploadString(url, "POST", json);
}
result looks like this:
{"Products":[{"ProductId":259959,"StockCount":83},{"ProductId":420124,"StockCount":158}]}
First I created a class:
public class ProductDetails
{
public string ProductId { get; set; }
public string StockCount { get; set; }
}
Then I tried to deserialize using this statement but couldn't understand.
var jsonresult = JsonConvert.DeserializeObject<ProductDetails>(result);
Debug.WriteLine(jsonresult.ProductId);
The above worked fine in visual basic with the following code but how to do this similar in C#
Dim Json As Object
Set Json = JsonConverter.ParseJson(xmlHttp.responseText)
For Each Product In Json("Products")
Debug.Print = Product("ProductId")
Debug.Print = Product("StockCount")
Next Product
You should use:
public class Product
{
public int ProductId { get; set; }
public int StockCount { get; set; }
}
public class RootObject
{
public List<Product> Products { get; set; }
}
var jsonresult = JsonConvert.DeserializeObject<RootObject>(result);
Because your JSON contains list of products, in jsonresult you have list of Product.
If you want get Product you can use eg. foreach
foreach(Product p in jsonresult.Products)
{
int id = p.ProductId;
}
Your JSON reads "an object that has a property named Products which contains an array of objects with properties ProductId and StockCount". Hence,
public class Inventory
{
public ProductDetails[] Products { get; set; }
}
var inventory = JsonConvert.DeserializeObject<Inventory>(result);
Your C# code cannot work because your json string contains values for 2 Product objects. As a result your var jsonresult variable will contain an array of Product objects, not one.
It is obvious in your VB code as you need to loop the Json variable in order to acquire each Product object.
Still your C# code would work if you string contained values for only one object like this:
{"ProductId" = 420124,"StockCount" = 158}
as you can see here http://www.newtonsoft.com/json/help/html/SerializingJSON.htm
Also you can try json parsing with JObject class, check this out: http://www.newtonsoft.com/json/help/html/t_newtonsoft_json_linq_jobject.htm

Array JSON deserialize

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.

Parsing Json facebook c#

I am trying for many hours to parse a JsonArray, I have got by graph.facebook, so that i can extra values. The values I want to extract are message and ID.
Getting the JasonArry is no Problem and works fine:
[
{
"code":200,
"headers":[{"name":"Access-Control-Allow-Origin","value":"*"}],
"body":"{
\"id\":\"255572697884115_1\",
\"from\":{
\"name\":\"xyzk\",
\"id\":\"59788447049\"},
\"message\":\"This is the first message\",
\"created_time\":\"2011-11-04T21:32:50+0000\"}"},
{
"code":200,
"headers":[{"name":"Access-Control-Allow-Origin","value":"*"}],
"body":"{
\"id\":\"255572697884115_2\",
\"from\":{
\"name\":\"xyzk\",
\"id\":\"59788447049\"},
\"message\":\"This is the second message\",
\"created_time\":\"2012-01-03T21:05:59+0000\"}"}
]
Now I have tried several methods to get access to message, but every method ends in catch... and throws an exception.
For example:
var serializer = new JavaScriptSerializer();
var result = serializer.Deserialize<dynamic>(json);
foreach (var item in result)
{
Console.WriteLine(item.body.message);
}
throws the exception: System.Collections.Generic.Dictionary doesnt contain definitions for body. Nevertheless you see in the screenshot below, that body contains definitions.
Becaus I am not allowed to post pictures you can find it on directupload: http://s7.directupload.net/images/120907/zh5xyy2k.png
I don't havent more ideas so i please you to help me. I need this for a project, private, not commercial.
Maybe you could give me an phrase of code, so i can continue my development.
Thank you so far
Dominic
If you use Json.Net, All you have to do is
replacing
var serializer = new JavaScriptSerializer();
var result = serializer.Deserialize<dynamic>(json);
with
dynamic result = JsonConvert.DeserializeObject(json);
that's all.
You are not deserializing to a strongly typed object so it's normal that the applications throws an exception. In other words, the deserializer won't create an Anynymous class for you.
Your string is actually deserialized to 2 objects, each containing Dictionary<string,object> elements. So what you need to do is this:
var serializer = new JavaScriptSerializer();
var result = serializer.Deserialize<dynamic>(s);
foreach(var item in result)
{
Console.WriteLine(item["body"]["message"]);
}
Here's a complete sample code:
void Main()
{
string json = #"[
{
""code"":200,
""headers"":[{""name"":""Access-Control-Allow-Origin"",""value"":""*""}],
""body"":{
""id"":""255572697884115_1"",
""from"":{
""name"":""xyzk"",
""id"":""59788447049""},
""message"":""This is the first message"",
""created_time"":""2011-11-04T21:32:50+0000""}},
{
""code"":200,
""headers"":[{""name"":""Access-Control-Allow-Origin"",""value"":""*""}],
""body"":{
""id"":""255572697884115_2"",
""from"":{
""name"":""xyzk"",
""id"":""59788447049""},
""message"":""This is the second message"",
""created_time"":""2012-01-03T21:05:59+0000""}}
]";
var serializer = new JavaScriptSerializer();
var result = serializer.Deserialize<dynamic>(json);
foreach(var item in result)
{
Console.WriteLine(item["body"]["message"]);
}
}
Prints:
This is the first message
This is the second message
I am using this simple technique
var responseTextFacebook =
#"{
"id":"100000891948867",
"name":"Nishant Sharma",
"first_name":"Nishant",
"last_name":"Sharma",
"link":"https:\/\/www.facebook.com\/profile.php?id=100000891948867",
"gender":"male",
"email":"nihantanu2010\u0040gmail.com",
"timezone":5.5,
"locale":"en_US",
"verified":true,
"updated_time":"2013-06-10T07:56:39+0000"
}"
I have declared a class
public class RootObject
{
public string id { get; set; }
public string name { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string link { get; set; }
public string gender { get; set; }
public string email { get; set; }
public double timezone { get; set; }
public string locale { get; set; }
public bool verified { get; set; }
public string updated_time { get; set; }
}
Now I am deserializing
JavaScriptSerializer objJavaScriptSerializer = new JavaScriptSerializer();
RootObject parsedData = objJavaScriptSerializer.Deserialize<RootObject>(responseTextFacebook );

Categories

Resources