C#如何使用SelectToken操作JSON

时间:2019-07-11 10:48:04

标签: c# json

我在string jsonContent中有JSON 首先,我必须检查它是JObject还是JArray

 private static bool IsJObject(string jsonContent)
        {
            return JToken.Parse(jsonContent) is JObject;
        }

然后:

if (IsJObject(jsonContent))
            {
                var jObject = ParseJObject(jsonContent);
                return GetFieldValueFromJObject(jObject, fieldPath);
            }
            else
            {
                var jArray = ParseJArray(jsonContent);
                return GetFieldValueFromJArray(jArray, fieldPath);
            }

如何使用JsonPath从JObject获取字段值,例如“名称” 如何使用JsonPath从JArray获取字段值。 “ [3]。名称” 如何从JObject和JArray更改字段值? 如何从JObject和JArray中删除字段?

我要获取的代码:

private static string GetFieldValueFromJObject(JObject jObject, string fieldPath)
        {
            return (string)jObject.SelectToken($"$.{fieldPath}");
        }

        private static string GetFieldValueFromJArray(JArray jArray, string fieldPath)
        {
            return (string)jArray.SelectToken($"$.{fieldPath}");
        }

对象json:

{
        "email": "testtest@fsafsf.com",
        "password": "testestest",
        "name": "Chelsey Dietrich",
        "username": "Kamren",
        "address": {
            "street": "Skiles Walks",
            "suite": "Suite 351",
            "city": "Roscoeview",
            "zipcode": "33263",
            "geo": {
                "lat": "-31.8129",
                "lng": "62.5342"
            }
        },
        "phone": "(254)954-1289",
        "website": "demarco.info",
        "company": {
            "name": "Keebler LLC",
            "catchPhrase": "User-centric fault-tolerant solution",
            "bs": "revolutionize end-to-end systems"
        }   
}

数组json:

[
    {
        "name": "test123",
        "username": "test",
        "phone": "4636346",
        "website": "sdgsdgsdg",
        "company": {
            "name": "sdgsdg",
            "catchPhrase": "dgsdg",
            "bs": "sdgsdg"
        },
        "id": 2
    },
    {
        "email": "testuser02@ddsfsdf.com",
        "password": "$2a$10$IIoTzjHSA07ZcWIbXRfv/OQhkxSDy/LHXQHdpcwMPgYPm97uTLwuG",
        "id": 3,
        "name": "testest1",
        "username": "Samantha",
        "address": {
            "street": "Douglas Extension",
            "suite": "Suite 847",
            "city": "McKenziehaven",
            "zipcode": "59590-4157",
            "geo": {
                "lat": "-68.6102",
                "lng": "-47.0653"
            }
        },
        "phone": "1-463-123-4447",
        "website": "ramiro.info",
        "company": {
            "name": "Romaguera-Jacobson",
            "catchPhrase": "Face to face bifurcated interface",
            "bs": "e-enable strategic applications"
        }
    }
]

1 个答案:

答案 0 :(得分:0)

简介: JArray具有JObjects列表,JArray和JObject都可以具有JProperty。 JObject可用作JProperty。 JArray,JObject,JProperty都源自JToken

要从JObject获取价值,请使用

jObject[propertyName]

从数组中获取价值

jArray[index][propertyName]

从jObject更改为jArray或相反都没有意义,它是jObject或jArray,虽然不能相同,但是请记住它们都是从JToken派生的。

要删除项目

foreach(JProperty prop in jObj)
        {
            if (prop.Name == "your prop name")
                prop.Remove();
        }