我的api返回
{
"result": [
{
"id": "51473",
"name": "serv-vc",
"modifydate": "2014-10-09 18:29:48.033",
"expirationoff": "false",
"createdate": "",
"scheduleoff": "false",
}
],
"status": 0
}
我已将其存储为JObject reponseobj
我无法弄清楚如何访问responseobj["result"][0]["id"]
每次我尝试它都会给出一个关于超出范围的数组。
我缺少什么?
我也试过
JArray resultarr = (JArray)responseobj.SelectToken("result");
resultarr[0]["id"]
但结果相同。
答案 0 :(得分:1)
假设响应位于名为response
的字符串变量中,则可以这样做:
JObject responseobj = JObject.Parse(response);
JObject result = (JObject)(responseobj.SelectToken("result") as JArray).First();
int id = result.Value<int>("id");
答案 1 :(得分:0)
不确定您的问题是什么,但这似乎对我有用:
static void Main(string[] args)
{
JObject j = JObject.Parse(
"{\"result\": [{\"id\": \"51473\", \"name\": \"serv-vc\", \"modifydate\": \"2014-10-09 18:29:48.033\", \"expirationoff\": \"false\", \"createdate\": \"\", \"scheduleoff\": \"false\", } ], \"status\": 0 }" );
var res = j["result"];
Console.Out.WriteLine(res);
// show an arrays
var maybe = j["result"][0];
Console.Out.WriteLine(maybe);
// shows the first object in the array
var fail = j["result"][0]["id"];
Console.Out.WriteLine(fail);
// shows 51473
}
答案 2 :(得分:0)
尝试使用:
JObject jObject = JObject.Parse( "{\"result\": [{\"id\": \"51473\", \"name\": \"serv-vc\", \"modifydate\": \"2014-10-09 18:29:48.033\", \"expirationoff\": \"false\", \"createdate\": \"\", \"scheduleoff\": \"false\", } ], \"status\": 0 }" );
要访问不同的节点,您可以使用:
string name = jObject["result"]["name"].ToString();
string expirationoff = jObject["result"]["expirationoff"].ToString();
或者你可以在一个新的json中转换result
要访问result
,您可以执行以下操作:
var result = jObject["result"][0];
请记住,你的json中可以有0,1,2 ... x个结果,那么你需要参考第一个位置。
答案 3 :(得分:0)
var Jobj = ((JObject)RequestObject)["data"];
foreach (JObject content in Jobj.Children<JObject>()) {
foreach (JProperty prop in content.Properties()) {
Console.WriteLine(prop.Name);//the column name
Console.WriteLine(prop.Value.ToString());// column value
}
}