parsing nested json array without labels in c# - c#

I have a nested json array looking like this:
[
[
[
[1234.5 ,9876,5],
[1234.5 ,9876,5]
],
[
[1234.5 ,9876,5],
[1234.5 ,9876,5]
]
],
[
[
[1234.5 ,9876,5],
[1234.5 ,9876,5]
],
[
[1234.5 ,9876,5],
[1234.5 ,9876,5]
]
]
]
I already saw many posts with answers if you have named keys in objects. However I just have large, nested array's. How should you make objects which can store this in c#? The length of the array's can vary.

List<List<List<List<int>>>>

Thanks for the start #user2033402
Using the package "NewtonSoft" i got it working using the following line as an example:
var results = Newtonsoft.Json.JsonConvert.DeserializeObject<List<List<List<List<double>>>>>(nestedJsonarray);

Related

How do I extract particular value based another another value?

so I want to be able to extract and ID based on whether that object has a particular property. I NEED this to be done via Regex. Here is an example of the JSON I am working with:
{
"workspaceid": ws01,
"data": {
"workspacetitle": "My Workspace"
},
"collections": {
"projects": [{
"id": 01,
"data": {
"title": "My Project 01",
"enddateperiod": "2020-02-20T23:59:59",
"profilecomplete": true,
"synced": false
},
"lists": {
"projectcode": [{
"id": pcodered,
"data": {
"code": "myproject123",
"name": "OffshoreProject"
}
}]
}
}, {
"id": 02,
"data": {
"title": "My Project 02",
"enddateperiod": "2020-02-20T23:59:59",
"profilecomplete": false,
"synced": false
},
"lists": {
"projectcode": [{
"id": pcodered,
"data": {
"code": "myproject123",
"name": "OffshoreProject"
}
}]
}
}]
}}
So what I want to extract is the ID of the project whose profile is not complete ("profilecomplete":false). So in this case, I want to select Project 2's id (which is 02).
How can I do this via Regex? I've managed to remove all of the whitespace and new lines as well so the JSON is essentially all one long line. Would it be easier to extract the Regex like this? Either way, I could use some help on how to get this ID.
NOTE: The format of the JSON cannot change.
This one works
/"id": ([^,]*?)(?=,[^{]*{[^}]*"profilecomplete": false)/
Explanations :
Read all these chars first "id":[space]
Then read in a group chars that aren't ","
And then a lookahead : you expect "," then chars that aren't "{", then "{"; and finally, before matching the closing "}", you want to read "profilecomplete": false
But I agree that a JSON parser would have been my preferred option!

Having issues reading json to string

Have tried several methods described but not winning. Here is the value ofthe var valuesEntity (this is from Luis integration into bot framework).
I have not found a way to interrogate this var so that I get a variable with the value of Venue - in this case Bakery.
{{
"$instance": {
"Venue": [
{
"startIndex": 13,
"endIndex": 19,
"text": "bakery",
"type": "Venue"
}
]
},
"Venue": [
[
"BAKERY"
]
]
}}
"Venue" is an string[][], Venue[0][0], should return "BAKERY".
This would be a valid json:
{
"$instance": {
"Venue": [{
"startIndex": 13,
"endIndex": 19,
"text": "bakery",
"type": "Venue"
}]
},
"Venue": [
[
"BAKERY"
]
]
}
You can check for valid json on https://jsonlint.com/
The key was to deserialize the Jason object to a Jason string.

How I can parse string variable like that

I need to parse this string with C# but don't know how. Would you advise me on a good way to get values from this string" Arrays "
["root",[["t-a2hICACK35I","isYktsZwEVMQ","r-isYktsZwEVMQ",[["global_config",null,null,null,null,[null,"[\"AOvVaw0H3zstE2R8Hh96uT8kZylb\\u0026ust\\u003d1511890102832262\",null,0,null,0,null,null,null,0,null,null,null,1,1,1,1,0,1,null,null,null,null,null,null,null,null,0,0,0,null,null,null,0,null,null,0,0,null,null,1,null,400,null,null,null,1,null,0,null,null,null,null,0,0,\"NONE\",null,4,\"Related image\",7,0,\"%1$d\\u0026nbsp;\\u0026#215;\\u0026nbsp;%2$d\",0,null,null,null,null,null,0,null,0,\"#222\",0,1,null,1,null,0,null,null,null,null,null,0,null,0,0,0]\n"]
]
,["group_config",null,null,null,null,[null,"[null,null,null,null,1,null,null,1]\n"]
]
,["image_group",null,null,null,null,[null,"[[[0,\"mdba4buxuK9BKM:\",[\"https://encrypted-tbn0.gstatic.com/images?q\\u003dtbn:ANd9GcRR2V_IL-Zh_LdrVzgBgvq6zcL68YSL01zNGsQJxXTo2cpXaqVz6A\",256,197]\n,[\"https://www.30grad.shop/item/images/11296/3000x3000/azet-kmngang-shirt-fastlife.jpg\",3000,2315]\n,null,0,{\"2001\":[]\n,\"2003\":[null,\"RXXHApKYqqq7RM\",\"https://www.30grad.shop/hersteller/kmn-gang/\",\"KMN Gang | 30° Shop\",\"KMN Gang Azet T-Shirt Fast Life\",null,null,null,null,null,null,null,\"30° Merchandise Shop\"]\n}]\n]\n]\n"]
]
]
]
,["t-cuCqGEujB5w","iP3_T8N4D_s8","r-iP3_T8N4D_s8",[["enable_close_for_background",null,null,null,null,[null,null,null,null,1]
]
,["initial_open",null,null,null,null,[null,null,null,null,null,0]
]
,["remain_in_lightbox_container",null,null,null,null,[null,null,null,null,0]
]
,["ux",null,null,null,null,[null,"[{\"220802553\":1}]\n"]
]
,["gsa",null,null,null,null,[null,"[{\"46740956\":0,\"244399487\":0}]\n"]
]
]
]
,["t-RHI35lUscno","igbzzOoE9k74","r-igbzzOoE9k74"]
,["t-3mFqq0A9uuY","iymxTruthWUk","r-iymxTruthWUk",[["hide_label_on_focus",null,null,null,null,[null,null,null,null,0]
]
]
]
,["t-mqWFpp0vPaI","iJcTl2Z4mNb0","r-iJcTl2Z4mNb0"]
,["t-mqWFpp0vPaI","iLi8ChEUFkT8","r-iLi8ChEUFkT8"]
,["t-mqWFpp0vPaI","iM4MogigWfMk","r-iM4MogigWfMk"]
]
]
The given string is a json array (not a json object)
You can use Newtonsoft.json (add the NuGet package to your solution) to parse it into a JArray and do whatever you want with it.
using System;
using Newtonsoft.Json.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var s = "[\"root\",[[\"t-a2hICACK35I\",\"isYktsZwEVMQ\",\"r-isYktsZwEVMQ\",[[\"global_config\",null,null,null,null,[null,\"[\\\"AOvVaw0H3zstE2R8Hh96uT8kZylb\\\\u0026ust\\\\u003d1511890102832262\\\",null,0,null,0,null,null,null,0,null,null,null,1,1,1,1,0,1,null,null,null,null,null,null,null,null,0,0,0,null,null,null,0,null,null,0,0,null,null,1,null,400,null,null,null,1,null,0,null,null,null,null,0,0,\\\"NONE\\\",null,4,\\\"Related image\\\",7,0,\\\"%1$d\\\\u0026nbsp;\\\\u0026#215;\\\\u0026nbsp;%2$d\\\",0,null,null,null,null,null,0,null,0,\\\"#222\\\",0,1,null,1,null,0,null,null,null,null,null,0,null,0,0,0]\\n\"]\r\n]\r\n,[\"group_config\",null,null,null,null,[null,\"[null,null,null,null,1,null,null,1]\\n\"]\r\n]\r\n,[\"image_group\",null,null,null,null,[null,\"[[[0,\\\"mdba4buxuK9BKM:\\\",[\\\"https://encrypted-tbn0.gstatic.com/images?q\\\\u003dtbn:ANd9GcRR2V_IL-Zh_LdrVzgBgvq6zcL68YSL01zNGsQJxXTo2cpXaqVz6A\\\",256,197]\\n,[\\\"https://www.30grad.shop/item/images/11296/3000x3000/azet-kmngang-shirt-fastlife.jpg\\\",3000,2315]\\n,null,0,{\\\"2001\\\":[]\\n,\\\"2003\\\":[null,\\\"RXXHApKYqqq7RM\\\",\\\"https://www.30grad.shop/hersteller/kmn-gang/\\\",\\\"KMN Gang | 30° Shop\\\",\\\"KMN Gang Azet T-Shirt Fast Life\\\",null,null,null,null,null,null,null,\\\"30° Merchandise Shop\\\"]\\n}]\\n]\\n]\\n\"]\r\n]\r\n]\r\n]\r\n,[\"t-cuCqGEujB5w\",\"iP3_T8N4D_s8\",\"r-iP3_T8N4D_s8\",[[\"enable_close_for_background\",null,null,null,null,[null,null,null,null,1]\r\n]\r\n,[\"initial_open\",null,null,null,null,[null,null,null,null,null,0]\r\n]\r\n,[\"remain_in_lightbox_container\",null,null,null,null,[null,null,null,null,0]\r\n]\r\n,[\"ux\",null,null,null,null,[null,\"[{\\\"220802553\\\":1}]\\n\"]\r\n]\r\n,[\"gsa\",null,null,null,null,[null,\"[{\\\"46740956\\\":0,\\\"244399487\\\":0}]\\n\"]\r\n]\r\n]\r\n]\r\n,[\"t-RHI35lUscno\",\"igbzzOoE9k74\",\"r-igbzzOoE9k74\"]\r\n,[\"t-3mFqq0A9uuY\",\"iymxTruthWUk\",\"r-iymxTruthWUk\",[[\"hide_label_on_focus\",null,null,null,null,[null,null,null,null,0]\r\n]\r\n]\r\n]\r\n,[\"t-mqWFpp0vPaI\",\"iJcTl2Z4mNb0\",\"r-iJcTl2Z4mNb0\"]\r\n,[\"t-mqWFpp0vPaI\",\"iLi8ChEUFkT8\",\"r-iLi8ChEUFkT8\"]\r\n,[\"t-mqWFpp0vPaI\",\"iM4MogigWfMk\",\"r-iM4MogigWfMk\"]\r\n]\r\n]";
var jArray = JArray.Parse(s);
Console.WriteLine(jArray.ToString());
}
}
}

C# Wrong order when writing json file [duplicate]

This question already has answers here:
Order of serialized fields using JSON.NET
(15 answers)
JSON order mixed up
(17 answers)
Closed 6 years ago.
I tryed to read a json file, edit it and then save it again, everything works fine except the order of the values in the new json file is wrong.
Here a part of the Original Json file:
"files": [
{
"name": "Game.cfg",
"sections": [
{
"name": "Chat",
"settings": [
{
"name": "ChatX",
"value": "44"
},
{
"name": "ChatY",
"value": "74"
},
{
"name": "Transparency",
"value": "0.0000"
}
]
},
and here the the same part in the new json file i create:
"files": [
{
"sections": [
{
"settings": [
{
"name": "ChatX",
"value": "44"
},
{
"name": "ChatY",
"value": "74"
},
{
"name": "Transparency",
"value": "0.0000"
}
],
"name": "Chat"
},
and here the code if needed:
string filename = filesource;
var res = JsonConvert.DeserializeObject<PersistentSettings>(File.ReadAllText(filename));
List<PersistentSettings> pers = new List<PersistentSettings>();
pers.Add(res);
string json = JsonConvert.SerializeObject(pers, Formatting.Indented);
//write string to file
File.WriteAllText("test.json", json);
like i said it works just not as it should, anyone got an idea why?

How to handle spaces in JSON keys when serializing to XML?

I'm using Json.NET in a .NET 4.0 application in order to convert a JSON RESTful response into XML. I am running into issues converting JSON into XML if a JSON child key has a space.
So far, I am able to convert most JSON responses.
Here are example responses along with the code which I am using to generate the XML.
{
num_reviews: "2",
page_id: "17816",
merchant_id: 7165
}
And here is the response which is causing an error:
[
{
headline: "ant bully",
created_date: "2010/06/12",
merchant_group_id: 10126,
profile_id: 0,
provider_id: 10000,
locale: "en_US",
helpful_score: 1314,
locale_id: 1,
variant: "",
bottomline: "Yes",
name: "Jessie",
page_id: "17816",
review_tags: [
{
Pros: [
"Easy to Learn",
"Engaging Story Line",
"Graphics",
"Good Audio",
"Multiplayer",
"Gameplay"
]
},
{
Describe Yourself: [
"Casual Gamer"
]
},
{
Best Uses: [
"Multiple Players"
]
},
{
Primary use: [
"Personal"
]
}
],
rating: 4,
merchant_id: 7165,
reviewer_type: "Verified Reviewer",
comments: "fun to play"
},
{
headline: "Ok game, but great price!",
created_date: "2010/02/28",
merchant_group_id: 10126,
profile_id: 0,
provider_id: 10000,
locale: "en_US",
helpful_score: 1918,
locale_id: 1,
variant: "",
bottomline: "Yes",
name: "Alleycatsandconmen",
page_id: "17816",
review_tags: [
{
Pros: [
"Easy to Learn",
"Engaging Story Line"
]
},
{
Describe Yourself: [
"Frequent Player"
]
},
{
Primary use: [
"Personal"
]
},
{
Best Uses: [
"Kids"
]
}
],
rating: 3,
merchant_id: 7165,
reviewer_type: "Verified Reviewer",
comments: "This is a cute game for the kids and at a great price. Just don't expect a whole lot."
}
]
So far, I have been considering on creating a mapping of the JSON data to a C# object and generating XML for that class. However, is there a way to keep this dynamic? Or is there a way to treat spaces as %20 encodings?
This question is same as how to validate JSON string before converting to XML in C#
If you have any further queries, please let me know.
You can call XmlConvert.EncodeName, which will escape any invalid characters using _s.
For example, a space would become _x0020_.
You cannot have an XMLElement Name with a space in it. You would need to replace the space with an Underscore or anyother element. If that is not feasible for you, try putting that value as an attribute for that Node.
I hope this makes sense.

Categories