How do I enumerate through a JObject?

前端 未结 4 2150
暗喜
暗喜 2020-11-28 05:52

I\'m trying to determine how to access the data that is in my JObject and I can\'t for the life of me determine how to use it.

JObject Object = (JObject)Resp         


        
4条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 06:41

    JObjects can be enumerated via JProperty objects by casting it to a JToken:

    foreach (JProperty x in (JToken)obj) { // if 'obj' is a JObject
        string name = x.Name;
        JToken value = x.Value;
    }
    

    If you have a nested JObject inside of another JObject, you don't need to cast because the accessor will return a JToken:

    foreach (JProperty x in obj["otherObject"]) { // Where 'obj' and 'obj["otherObject"]' are both JObjects
        string name = x.Name;
        JToken value = x.Value;
    }
    

提交回复
热议问题