我如何从json获得某些东西的价值?

时间:2014-03-21 06:42:32

标签: c# asp.net json visual-studio-2010

我有以下json字符串。

{
"items": {
    "642163": {
        "id": 642163,
        "nm": "AK-21699-11-Lancer-Mohammed Al Noman",
        "uid": "356307041429068",
        "ph": "+971561755362",
        "hwid": 216940,
        "pos_x": 55.3744512,
        "pos_y": 25.335552,
        "pos_z": 50,
        "pos_t": 1395383860,
        "pos_sc": 7,
        "hdop": 1.3,
        "lmsg": {
            "t": 1395383860,
            "f": 3,
            "tp": "ud",
            "pos": {
                "y": 25.335552,
                "x": 55.3744512,
                "z": 50,
                "s": 0,
                "c": 0,
                "sc": 7
            },
            "i": 0,
            "p": {
                "param179": 0,
                "param180": 1,
                "param69": 1,
                "param175": 1,
                "param182": 13,
                "hdop": 1.3,
                "pwr_ext": 12.978,
                "battery_charge": 0
            }
        },
        "param180": 1,
        "param69": 1,
        "param182": 13,
        "pwr_ext": 12.978,
        "cnm": 71869,
        "cnkb": 2400,
        "cneh": 2160,
        "client_name": "Al Kharafi",
        "access_code": "sms,sms",
        "serial_no": "2837965",
        "vreg_number": "21699",
        "instln_date": "1/14/2013",
        "vehicle_type": "car_saloon",
        "contact_ph": "branches"
    }
},
"sid": "b96c9de5b4c609f905219c20b113f712",
"count": 12,
"p_type": "hst"
}

我想得到id,nm,p和pos.How我会在asp.net c#中得到它吗?

1 个答案:

答案 0 :(得分:2)

抓住JSON.NET库。它可以通过NuGet以该名称获得。

using Newtonsoft.Json.Linq;

现在,您可以从JSON字符串yourText中提取数据,如下例所示。

var token = JToken.Parse(yourText);
var nm = token["items"]["642163"]["nm"].ToObject<string>();

nm的值为AK-21699-11-Lancer-Mohammed Al Noman