Это вопрос, основанный на Как перебирать словарь для получения и передачи имени ключа в строку , приведенный ниже код перебирает JSON, получая имена ключей и индекс JArray и передавая их упорядоченнок строкам путей JSON, наконец, он возвращает словарь (упорядоченная строка, JsonValue), ключ-имя словаря, как ожидается, будет упорядочен как «ключ1: ключ1-1: 0», что означает требуемый словарь [»ключ1: ключ1-1:0 "] = commonDictionary [" key1 "] [" key1-1 "] [0].
В соответствии с JSON ниже, если" Five ": {" ArrayInFive ": [" elem1 "," elem2"]} удален, работает нормально.
C # код
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
......
static void Main(string[] args)
{
var json = File.ReadAllText(@myJsonPath);
var jObj = JsonConvert.DeserializeObject<JObject>(json);
var desiredDict = FlattenJObjectToDictionary(jObj);
foreach (var key in desiredDict.Keys)
{
Console.WriteLine(key + " : " + desiredDict[key]);
}
Console.Read();
}
private static IDictionary<string, string> FlattenJObjectToDictionary(JObject obj)
{
// obtain a key/value enumerable and convert it to a dictionary
return NestedJObjectToFlatEnumerable(obj, null).ToDictionary(kv => kv.Key, kv => kv.Value);
}
private static IEnumerable<KeyValuePair<string, string>> NestedJObjectToFlatEnumerable(object data, string path = null)
{
JObject jObject = (JObject)data;
var jOP = jObject.Properties();
foreach (var jop in jOP)
{
if (jop.Value is JObject)
{
var child = (JObject)jop.Value;
// build the child path based on the root path and the property name
string childPath = path != null ? string.Format("{0}{1}:", path, jop.Name) : string.Format("{0}:", jop.Name);
// get each result from our recursive call and return it to the caller
foreach (var resultVal in NestedJObjectToFlatEnumerable(child, childPath))
{
yield return resultVal;
}
}
else if (jop.Value is JArray)
{
var jArray = (JArray)jop.Value;
for (int i = 0; i < jArray.Count; i++)
{
var child = jArray[i];
// build the child path based on the root path and the JArray index
string childPath = path != null ? string.Format("{0}{1}:{2}:", path, jop.Name, i.ToString()) : string.Format("{0}:{1}:", jop.Name, i.ToString());
// get each result from our recursive call and return it to the caller
foreach (var resultVal in NestedJObjectToFlatEnumerable(child, childPath))
{
yield return resultVal;
}
}
}
else
{
// this kind of assumes that all values will be convertible to string, so you might need to add handling for other value types
yield return new KeyValuePair<string, string>(string.Format("{0}{1}", path, Convert.ToString(jop.Name)), Convert.ToString(jop.Value));
}
}
}
JSON
{
"One": "Hey",
"Two": {
"Two": "HeyHey"
},
"Three": {
"Three": {
"Three": "HeyHeyHey"
}
},
"Four": [
{
"One": "Hey"
},
{
"Two":
{
"Two": "HeyHey"
}
}
],
"Five": {
"ArrayInFive": [ "elem1", "elem2" ]
}
}
Iожидаем
wantedDictionary ["Five"] ["ArrayInFive"] [0] = "elem1"
и
wantedDictionary ["Five"] ["ArrayInFive"] [1] = "elem2"
Но за исключением того, что "невозможно конвертировать JValue в JObject", мне нужна помощь с исправлением кода, может быть, всей программы.