Convert any Json string dynamically to Dataset - c#

I have a Json string that comes from another project to my C# project.
I want to convert this object to Dataset (list of Datatables).
I got this error:
Additional text found in JSON string after finishing deserializing object.
This is my sample json:
{
"list":[
[
256,
181940,
635346,
0,
1,
5696109,
18350,
0,
0,
"232",
1
],
[
"id",
"Factorid",
"classid",
"hostid",
"ownerid",
"orderid",
"userid",
"LastOrder",
"NextOrder",
"note",
"used"
]
],
"list2":[
[
"ID",
"MID",
"OwnerID",
"Ord",
"Enabled",
"schemaID",
"LastUpdate",
"createDate",
"parentid",
"General"
],
[
1207054,
20,
1,
1000,
1,
40,
"2015-01-11 03:18:24.143000",
"2015-01-08 22:55:17.930000",
0,
0
],
[
1207126,
20,
1,
1000,
1,
40,
"2015-01-12 02:59:15.227000",
"2015-01-12 01:37:10.317000",
0,
0
]
]
}
It's ok and will be converted to JObject with this method:
JObject jo = JObject.Parse(jsonString);
I've searched stackoverflow but didn't find any answer that was solved.
Everybody says first define a class that maps to json object, but json comes to my project,
may have different structures which in many situations won't map to class created for it.
The question is how can I dynamically convert every json to Dataset which comes to my project?

Related

Parsing a lot of dynamic data from json file

I'm fairly new to C# and a lot of the libraries, but I'm trying to deserialize a json file that has a bunch of non-dynamic fields followed by a bunch of dynamically named structures with more dynamically sized/named structures underneath them.
Example of json:
"Header": {
"date": "Aug 30 2020",
"time": "14:00:00"
},
...
Other fields that will remain static
...
// Start of fields that can grow/shrink and be added to/deleted from
"Birthday": {
"MessageIndex": 0,
"Info Array Size": 3,
"Info": {
"Date": 30,
"Month": 8,
"Year": 2020
},
},
"Time of Day": {
"MessageIndex": 1,
"Info Array Size": 3,
"Info": {
"Hour": 2,
"Minute": 0,
"Second": 0
},
},
"Height": {
"MessageIndex": 2,
"Info Array Size": 2,
"Info": {
"Feet": 6,
"Inches": 2
},
},
// Continues on for any number of messages
I'm looking to create a tool to be able to parse and document what is in this information without having to add a JsonConverter for each new thing that gets added. Instead I'm looking for something that can
parse any number of items (like "Birthday", "Time of Day", "Height") that have an unknown name and a field named "Info" underneath it with an unknown number of fields and each of those fields name is unknown at the time of running the tool.
I was looking to use JSON.net (or any other available library really), but if that doesn't work I'm fine parsing the file manually. I'd just rather avoid that if possible. Thanks!

How do you dynamically parse, read, and append to JSON with multiple sub arrays

After a bunch of research I haven't been able to find any good ways to read a json file, store their values, then append a new object/array to it.
The JSON looks like
{
"Skywars": [
{
"Solo Normal": [
{
"000001": [
{
"Kills": 213,
"Deaths": 117
}
]
}
],
"Solo Insane": [
{
"000001": [
{
"Kills": 10790,
"Deaths": 7184
}
]
}
]
}
],"Bedwars": [
{
"Solo": [
{
"000001": [
{
"Kills": 0,
"Deaths": 0
}
]
}
],
"Duos": [
{
"000001": [
{
"Kills": 0,
"Deaths": 0
}
]
}
]
}
]
}
As an example, i am intending for it to go "Skywars.Solo Normal", "Skywars.Solo Insane", "Bedwars.Solo", "Bedwars.Duos" then append "000002" with new kills and deaths values.
For some reason, even after hours of searching, I can't find out how to read the kills and deaths (I've gotten close, using public Skywars[] Skywars { get;set; }. Problem is most of the examples are using JSON files that look like {"user":[{"id":1,"logins":0}]} with very little arrays & sub arrays.
To anyone who is kind enough to answer, please don't spoonfeed me code, explain how it would be done (would I need to create my own parser, etc), or if there are already any posts/links that answer my question (even though I failed to find how).
Notes -
"000001" and "000002" will be dynamic, so each time you start the program those values will be different. I just want to append after the last instance of stats saved.
Also sorry, I am still learning C# but know most of the basics and some more complex concepts, I've just never been good at storing data and using JSON. If you need anything to help else just add a comment and I'll add it.
Please see Json.NET's Modifying JSON.
This sample loads JSON, modifies JObject and JArray instances and then writes the JSON back out again.
Sample
string json = #"{
'channel': {
'title': 'Star Wars',
'link': 'http://www.starwars.com',
'description': 'Star Wars blog.',
'obsolete': 'Obsolete value',
'item': []
}
}";
JObject rss = JObject.Parse(json);
JObject channel = (JObject)rss["channel"];
channel["title"] = ((string)channel["title"]).ToUpper();
channel["description"] = ((string)channel["description"]).ToUpper();
channel.Property("obsolete").Remove();
channel.Property("description").AddAfterSelf(new JProperty("new", "New value"));
JArray item = (JArray)channel["item"];
item.Add("Item 1");
item.Add("Item 2");
Console.WriteLine(rss.ToString());
// {
// "channel": {
// "title": "STAR WARS",
// "link": "http://www.starwars.com",
// "description": "STAR WARS BLOG.",
// "new": "New value",
// "item": [
// "Item 1",
// "Item 2"
// ]
// }
// }

JSON Value (UWP C#)

I writing app for Windows 10.
I have connection with WooCommerce with WooCommerce.Net plugin
Here it is plugin
I have JSON string.
Code:
RestAPI rest = new RestAPI("http://simplegames.com.ua/wp-json/wc/v1/", "ck_9d64c027d2c5f81b8bed3342eeccc6d337be813d", "cs_60697b1e6cbdeb8d62d19e0765e339f8e3334754");
WCObject wc = new WCObject(rest);
//Получение заказов
var orders = await wc.GetOrders()
string products = orders[1].line_items.ToFormattedJsonString();
Debug.WriteLine(products);
I have this JSON in Console
[
{
"id": 72,
"name": "Із лосося",
"sku": "344",
"product_id": 1134,
"variation_id": 0,
"quantity": 1,
"tax_class": "",
"price": 75.00,
"subtotal": 75.00,
"subtotal_tax": 0.00,
"total": 75.00,
"total_tax": 0.00,
"taxes": [
],
"meta": [
]
},
{
"id": 73,
"name": "Італьяно",
"sku": "340",
"product_id": 1138,
"variation_id": 0,
"quantity": 1,
"tax_class": "",
"price": 38.00,
"subtotal": 38.00,
"subtotal_tax": 0.00,
"total": 38.00,
"total_tax": 0.00,
"taxes": [
],
"meta": [
]
}
]
I need to write value from this JSON
for example name to variable.
How I can do this?
It depends on the order line item from where you want to write value from. Assuming int X and int Y to be less than equal to orders.count, orders.line_items.count respectively, You can get the values directly with orders[X].line_items[Y].Propertyname
A better idea would be to create classes representing the business objects and then translating property values into your objects and using it for your application purposes.

How To Serialize JSON Dynamically Sized Multi-dimensional Array From MongoDB

I have some coordinates that I'm reading from a MongoDB using the 10Gen driver. The problem I'm running into is that the depth of the multi-dimensional data isn't consistent and I'm looking for a comprehensive strategy to handle the problem.
The solution must be able to write the coordinates back to MongoDB (serialize) in the same order and depth as read out (deserialized).
Two examples of different depth:
Example 1
[
[ [ [ 12, 33 ], [ 32, 23 ], [ 12, 32], [23, 12 ], [ 32, 32 ], [ 32, 2 ] ] ],
[ [ [35, 12 ], [ 53, 16 ], [ 22, 54 ], [ 2, 32 ], [ 32, 32 ] ] ]
]
Deserialize into:
public List<List<List<int>>> coordinates { get; set; }
Example 2
[
[ [ 2, 2 ], [120,12 ], [ 32,32 ], [ 32, 2 ], [ 3,2 ], [ 2, 3 ], [ 2, 1 ] ]
]
Deserialize into:
public List<List<List<List<int>>>> coordinates { get; set; }
My initial thought was to write a custom serializer to read the data and figure out the depth, then use a wrapper method to hide the actual data from the View e.g. GetNextCoordinate() and then the function GetNextCoordinate can pull the data from the appropriate structure. That feels messy so I'm looking for a more generic solution.
Assume that the coordinates property is part of a concrete class and that the collection in MongoDB contains a mix of both types of coordinate documents. Meaning any given query to MongoDB will pull either or both type of coordinate documents and I don't know before executing the query.
Maybe try http://www.codeproject.com/Articles/159450/fastJSON
It can serialize and deserialize to lists. I don't know if it can do lists within lists.
I ended up writing a custom BSON serializer/deserializer which then supports variable nested arrays.

Get JSON object node

I have a json object and I want to get the value of :
entities > media > sizes > large > h
Is there a way to get it like XML -> Xpath method?
This is extra lines that is irrelevant to question just because of ...
{
"created_at": "Sun, 01 Jan 2012 17:05:32 +0000",
"entities": {
"user_mentions": [
{
"screen_name": "nurdanterbiyik",
"name": "nurdan",
"id": 264782080,
"id_str": "264782080",
"indices": [
0,
15
]
}
],
"media": [
{
"id": 153522253777219584,
"id_str": "153522253777219584",
"indices": [
44,
64
],
"media_url": "http://p.twimg.com/AiFrrSmCMAAdEID.jpg",
"media_url_https": "https://p.twimg.com/AiFrrSmCMAAdEID.jpg",
"url": "http://t.co/ZwHN9gvO",
"display_url": "pic.twitter.com/ZwHN9gvO",
"expanded_url": "http://twitter.com/emelkiraac/status/153522253773025280/photo/1",
"type": "photo",
"sizes": {
"large": {
"w": 536,
"h": 800,
"resize": "fit"
},
Using JSON.NET you have several ways to read data without having to deserialize your JSON text into objects. Here is a simplified example:
string json = #" {
""created_at"": ""Sun, 01 Jan 2012 17:05:32 +0000"",
""entities"": {
""media"": [{
""type"": ""photo"",
""sizes"": {
""large"": {
""w"": 536,
""h"": 800,
""resize"": ""fit""
}
}
}]
}
}
";
JObject o = JObject.Parse(json);
int h = (int)o["entities"]["media"][0]["sizes"]["large"]["h"];
int h2 = (int)o.SelectToken("entities.media[0].sizes.large.h");
I prefer to think of JSON purely as serialized objects--the first and last thing I want to do with it is to de-serialize it into the objects that I then work with.
I can't tell you the best way to do it in c#, but I've had good luck using JSON.NET
JSON.NET will also let you (quote from website) "LINQ to JSON is good for situations where you are only interested in getting values from JSON, you don't have a class to serialize or deserialize to, or the JSON is radically different from your class and you need to manually read and write from your objects. LINQ to JSON allows you to easily read, create and modify JSON in .NET.", which sounds like what you are trying to do. I've never tried it, but if all you're trying to do is get one piece of data out of that long string, then it sounds like a good alternative.

Categories

Resources