現有以下數據結構:javascript
{
"orderId":
"000001"
,
"goodsId[0]"
:
"001"
,
"goodsId[2]"
:
"002"
,
"goodsId[3]"
:
"003"
,
}
goodsId 是無限延伸下去的,問如何解析。java
顯然,定義類而後解析的思路確定行不通,即便用dynamic(不須要定義實體類,直接取對應key的值),如: dynamic obj = JsonConvert.DeserializeObject(json); 這樣也不行,緣由是obj.orderId能夠表示,但後面的帶有[]的,沒法取出。json
所以咱們必需要換個思路。經過轉換爲JObject對象的屬性Property,獲取對應的Value,所以代碼以下。數據結構
string json = "{\"orderId\":\"000001\",\"goodsId[0]\":\"001\",\"goodsId[3]\":\"003\",\"goodsId[10]\":\"new data\"}"; JObject jsonObj = JsonConvert.DeserializeObject(json) as JObject; foreach (var p in jsonObj.Properties().ToArray()) Console.WriteLine("key={0}, value={1}", p.Name, jsonObj[p.Name].Value<string>());