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());
}
}
}
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.