こんにちは私は、jsonオブジェクトの与えられたjson配列をトラバースし、各jsonオブジェクトの "F"プロパティのバックスラッシュ(/)の数に基づいて各jsonオブジェクトに動的に "深さ"私のプロジェクトに必要なものです。ここでjson文字列(jsonオブジェクトのjson配列)をトラバースし、C#を使用して動的にプロパティを追加するにはどうすればよいですか?
は、JSON文字列です:
[
{
"A":"0",
"B":"0",
"ID":"000001",
"C":"Class",
"D":"/000001",
"F":"/Class",
"children":[
{
"A":"1",
"B":"1",
"ID":"000004",
"C":"Test1",
"D":"/000001/000004",
"F":"/Class/Test1"
},
{
"A":"0",
"B":"0",
"ID":"000005",
"C":"aaa1_test_2",
"D":"/000001/000005",
"F":"/Class/aaa1_test_2",
"children":[
{
"A":"1",
"B":"1",
"ID":"000006",
"C":"abcd",
"D":"/000001/000005/000006",
"F":"/Class/aaa1_test_2/abcd"
}
]
},
{
"A":"1",
"B":"1",
"ID":"000007",
"C":"16.2.2017",
"D":"/000001/000007",
"F":"/Class/16.2.2017"
},
{
"A":"0",
"B":"1",
"ID":"000008",
"C":"008000_02-Core Dia:-CORE DIAMETER",
"D":"/000001/000008",
"F":"/Class/008000_02-Core Dia:-CORE DIAMETER",
"children":[
{
"A":"1",
"B":"1",
"ID":"000027",
"C":"1wa",
"D":"/000001/000008/000027",
"F":"/Class/008000_02-Core Dia:-CORE DIAMETER/@1wa"
}
]
},
{
"A":"0",
"B":"1",
"ID":"000009",
"C":"20.2.2017",
"D":"/000001/000009",
"F":"/Class/20.2.2017",
"children":[
{
"A":"1",
"B":"1",
"ID":"000010",
"C":"TEST-005",
"D":"/000001/000009/000010",
"F":"/Class/20.2.2017/TEST-005"
}
]
},
{
"A":"1",
"B":"1",
"ID":"000011",
"C":"3.3.2017",
"D":"/000001/000011",
"F":"/Class/3.3.2017"
},
{
"A":"1",
"B":"1",
"ID":"000016",
"C":"TEST100",
"D":"/000001/000016",
"F":"/Class/TEST100"
},
{
"A":"1",
"B":"1",
"ID":"000017",
"C":"Test101",
"D":"/000001/000017",
"F":"/Class/Test101"
},
{
"A":"1",
"B":"1",
"ID":"000019",
"C":"Test102",
"D":"/000001/000019",
"F":"/Class/Test102"
},
{
"A":"1",
"B":"1",
"ID":"000024",
"C":"Godrej",
"D":"/000001/000024",
"F":"/Class/God"
},
{
"A":"1",
"B":"1",
"ID":"000025",
"C":"T",
"D":"/000001/000025",
"F":"/Class/T"
},
{
"A":"1",
"B":"1",
"ID":"000026",
"C":"K_Test",
"D":"/000001/000026",
"F":"/Class/K_Test"
},
{
"A":"1",
"B":"0",
"ID":"000028",
"C":"RB_TEST1",
"D":"/000001/000028",
"F":"/Class/RB_TEST1"
},
{
"A":"0",
"B":"0",
"ID":"900004",
"C":"Comps",
"D":"/000001/900004",
"F":"/Class/Comps",
"children":[
{
"A":"0",
"B":"1",
"ID":"900006",
"C":"Capacitors",
"D":"/000001/900004/900006",
"F":"/Class/Comps/Capacitors",
"children":[
{
"A":"1",
"B":"1",
"ID":"000015",
"C":"Test2",
"D":"/000001/900004/900006/000015",
"F":"/Class/Comps/Capacitors/Test2"
},
{
"A":"0",
"B":"1",
"ID":"000018",
"C":"Test3",
"D":"/000001/900004/900006/000018",
"F":"/Class/Comps/Capacitors/Test3",
"children":[
{
"A":"1",
"B":"0",
"ID":"000020",
"C":"Test4",
"D":"/000001/900004/900006/000018/000020",
"F":"/Class/Comps/Capacitors/Test3/Test4"
}
]
}
]
},
{
"A":"1",
"B":"1",
"ID":"900007",
"C":"Induct",
"D":"/000001/900004/900007",
"F":"/Class/Comps/Induct"
},
{
"A":"1",
"B":"1",
"ID":"900008",
"C":"ICS",
"D":"/000001/900004/900008",
"F":"/Class/Comps/ICS"
}
]
}
]
}
]
私はこのような出力必要があります:あなたが最初のJSONオブジェクトを見れば "F" プロパティは、1が含まれています。例えば
[
{
"A":"0",
"B":"0",
"ID":"000001",
"C":"Class",
"D":"/000001",
"depth":1,
"F":"/Class",
"children":[
{
"A":"1",
"B":"1",
"ID":"000004",
"C":"Test1",
"D":"/000001/000004",
"depth":2,
"F":"/Class/Test1"
},
{
"A":"0",
"B":"0",
"ID":"000005",
"C":"aaa1_test_2",
"D":"/000001/000005",
"depth":2,
"F":"/Class/aaa1_test_2",
"children":[
{
"A":"1",
"B":"1",
"ID":"000006",
"C":"abcd",
"D":"/000001/000005/000006",
"depth":3,
"F":"/Class/aaa1_test_2/abcd"
}
]
},
{
"A":"1",
"B":"1",
"ID":"000007",
"C":"16.2.2017",
"D":"/000001/000007",
"depth":2,
"F":"/Class/16.2.2017"
},
{
"A":"0",
"B":"1",
"ID":"000008",
"C":"008000_02-Core Dia:-CORE DIAMETER",
"D":"/000001/000008",
"depth":2,
"F":"/Class/008000_02-Core Dia:-CORE DIAMETER",
"children":[
{
"A":"1",
"B":"1",
"ID":"000027",
"C":"1wa",
"D":"/000001/000008/000027",
"depth":3,
"F":"/Class/008000_02-Core Dia:-CORE DIAMETER/@1wa"
}
]
},
{
"A":"0",
"B":"1",
"ID":"000009",
"C":"20.2.2017",
"D":"/000001/000009",
"depth":2,
"F":"/Class/20.2.2017",
"children":[
{
"A":"1",
"B":"1",
"ID":"000010",
"C":"TEST-005",
"D":"/000001/000009/000010",
"depth":3,
"F":"/Class/20.2.2017/TEST-005"
}
]
},
{
"A":"1",
"B":"1",
"ID":"000011",
"C":"3.3.2017",
"D":"/000001/000011",
"depth":2,
"F":"/Class/3.3.2017"
},
{
"A":"1",
"B":"1",
"ID":"000016",
"C":"TEST100",
"D":"/000001/000016",
"depth":2,
"F":"/Class/TEST100"
},
{
"A":"1",
"B":"1",
"ID":"000017",
"C":"Test101",
"D":"/000001/000017",
"depth":2,
"F":"/Class/Test101"
},
{
"A":"1",
"B":"1",
"ID":"000019",
"C":"Test102",
"D":"/000001/000019",
"depth":2,
"F":"/Class/Test102"
},
{
"A":"1",
"B":"1",
"ID":"000024",
"C":"Godrej",
"D":"/000001/000024",
"depth":2,
"F":"/Class/God"
},
{
"A":"1",
"B":"1",
"ID":"000025",
"C":"T",
"D":"/000001/000025",
"depth":2,
"F":"/Class/T"
},
{
"A":"1",
"B":"1",
"ID":"000026",
"C":"K_Test",
"D":"/000001/000026",
"depth":2,
"F":"/Class/K_Test"
},
{
"A":"1",
"B":"0",
"ID":"000028",
"C":"RB_TEST1",
"D":"/000001/000028",
"depth":2,
"F":"/Class/RB_TEST1"
},
{
"A":"0",
"B":"0",
"ID":"900004",
"C":"Comps",
"D":"/000001/900004",
"depth":2,
"F":"/Class/Comps",
"children":[
{
"A":"0",
"B":"1",
"ID":"900006",
"C":"Capacitors",
"D":"/000001/900004/900006",
"depth":3,
"F":"/Class/Comps/Capacitors",
"children":[
{
"A":"1",
"B":"1",
"ID":"000015",
"C":"Test2",
"D":"/000001/900004/900006/000015",
"depth":4,
"F":"/Class/Comps/Capacitors/Test2"
},
{
"A":"0",
"B":"1",
"ID":"000018",
"C":"Test3",
"D":"/000001/900004/900006/000018",
"depth":4,
"F":"/Class/Comps/Capacitors/Test3",
"children":[
{
"A":"1",
"B":"0",
"ID":"000020",
"C":"Test4",
"D":"/000001/900004/900006/000018/000020",
"depth":5,
"F":"/Class/Comps/Capacitors/Test3/Test4"
}
]
}
]
},
{
"A":"1",
"B":"1",
"ID":"900007",
"C":"Induct",
"D":"/000001/900004/900007",
"depth":2,
"F":"/Class/Comps/Induct"
},
{
"A":"1",
"B":"1",
"ID":"900008",
"C":"ICS",
"D":"/000001/900004/900008",
"depth":2,
"F":"/Class/Comps/ICS"
}
]
}
]
}
]
をバック」そうスラッシュ「深さ」プロパティの値は1にする必要があります。
_「ここにコードがあります」_ - あなたはコードを投稿しませんでした。 JSONデータ。明白なアプローチは、ソースデータを実際のオブジェクトモデルに非直列化し、新しい 'depth'プロパティでJSONとしてデータを再整理することです。最初にJSONを処理するために使用しているものに応じて、 'depth'プロパティ(初期化されていない状態)でデシリアライズしたり、プロパティを設定してから再シリアル化したり、あなたが望む値に設定したプロパティを持つ別のものへのプロパティを持たない構造体。 –