假设我从Web服务获得以下json数据,我无法更改。
[
[
"Header1",
"Header2",
"Header3",
"Header4"
],
[
"FirstValue1",
"FirstValue2",
"FirstValue3",
"FirstValue4"
],
[
"SecondValue1",
"SecondValue2",
"SecondValue3",
"SecondValue4"
]
]
jsonlint.com告诉我它是有效的json,据我所知,我会同意。
但不知怎的,我想知道是否有任何“简单”的方法将其反序列化为一个类。第二个和第三个数组中的每个值都属于第一个数组中的相应头。
我知道如何使用Json.NET,但无法理解如何在数据结构中使用它。
答案 0 :(得分:9)
简单 - 您可以使用JsonConvert.DeserializeObject
将其反序列化为string[][]
:
using System;
using System.IO;
using Newtonsoft.Json;
class Test
{
static void Main()
{
var json = File.ReadAllText("test.json");
string[][] array = JsonConvert.DeserializeObject<string[][]>(json);
Console.WriteLine(array[1][3]); // FirstValue4
}
}
答案 1 :(得分:3)
最简单的方法是使用$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
if (toState.$$finishAuthorize) {
return;
}
if (!authenticated) {
event.preventDefault();
toState = angular.extend({'$$finishAuthorize': true}, toState);
// following $timeout is emulating a backend $http.get('/auth/') request
$timeout(function() {
authenticated = true;
$state.go(toState.name, toParams, {notify: false}).then(function() {
$rootScope.$broadcast('$stateChangeSuccess', toState, toParams, fromState, fromParams);
});
},1000)
}
);
类,并使用string
对其进行反序列化。
Json.NET
答案 2 :(得分:-1)
更好的选择可能是使用
int all = smalldrinkcounter + changestate + keypadvalue;
来源:http://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_Linq_JObject_Parse.htm