我是处理JSON数据的新手。
我正在从web服务读取数据。返回的查询数据如下:
[["B02001_001E","NAME","state"],
["4712651","Alabama","01"],
["691189","Alaska","02"],
["6246816","Arizona","04"],
["18511620","Florida","12"],
["9468815","Georgia","13"],
["1333591","Hawaii","15"],
["1526797","Idaho","16"],
["3762322","Puerto Rico","72"]]
有没有一种方法可以反序列化这些数据,从而在不需要我首先定义对象的情况下生成基本对象?在上面的例子中,对象由第一行定义:
["B02001_001E","NAME","state"],
通常,web服务将返回格式化为二维JSON数组的查询数据,其中第一行提供列名,随后的行提供数据值。
您可以非常容易地反序列化它。C#中的数据结构只是List<string[]>
,所以您可以这样做;
List<string[]> data = JsonConvert.DeserializeObject<List<string[]>>(jsonString);
上面的代码假设您使用的是json。NET。
EDIT:注意json在技术上是一个字符串数组的数组。我更喜欢将List<string[]>
用于我自己的声明,因为它更直观。它不会给json带来任何问题。NET,如果你想让它是一个字符串数组的数组,那么你需要把类型改为(我认为)string[][]
,但在C#中有一些有趣的小问题,比如锯齿状和2D数组,我真的不知道,所以我在这里不必处理它。
如果你使用.Net 4.5,你也可以使用标准的.Net json序列化程序:
using System.Runtime.Serialization.Json;
...
Stream jsonSource = ...; // serializer will read data stream
var s = new DataContractJsonSerializer(typeof(string[][]));
var j = (string[][])s.ReadObject(jsonSource);
在.Net 4.5及更早版本中,您可以使用JavaScriptSerializer类:
using System.Web.Script.Serialization;
...
JavaScriptSerializer serializer = new JavaScriptSerializer();
string[][] list = serializer.Deserialize<string[][]>(json);
步骤1:转到json.org查找用于调用此web服务的任何技术的json库。下载并链接到该库。
第2步:假设您正在使用Java。你可以这样使用JSONArray:
JSONArray myArray=new JSONArray(queryResponse);
for (int i=0;i<myArray.length;i++){
JSONArray myInteriorArray=myArray.getJSONArray(i);
if (i==0) {
//this is the first one and is special because it holds the name of the query.
}else{
//do your stuff
String stateCode=myInteriorArray.getString(0);
String stateName=myInteriorArray.getString(1);
}
}
您可以编写自己的JSON解析器,并根据您的需求使其更通用。这是一个很好地满足了我的目的,希望也能帮助你。
class JsonParsor
{
public static DataTable JsonParse(String rawJson)
{
DataTable dataTable = new DataTable();
Dictionary<string, string> outdict = new Dictionary<string, string>();
StringBuilder keybufferbuilder = new StringBuilder();
StringBuilder valuebufferbuilder = new StringBuilder();
StringReader bufferreader = new StringReader(rawJson);
int s = 0;
bool reading = false;
bool inside_string = false;
bool reading_value = false;
bool reading_number = false;
while (s >= 0)
{
s = bufferreader.Read();
//open JSON
if (!reading)
{
if ((char)s == '{' && !inside_string && !reading)
{
reading = true;
continue;
}
if ((char)s == '}' && !inside_string && !reading)
break;
if ((char)s == ']' && !inside_string && !reading)
continue;
if ((char)s == ',')
continue;
}
else
{
if (reading_value)
{
if (!inside_string && (char)s >= '0' && (char)s <= '9')
{
reading_number = true;
valuebufferbuilder.Append((char)s);
continue;
}
}
//if we find a quote and we are not yet inside a string, advance and get inside
if (!inside_string)
{
if ((char)s == '"' && !inside_string)
inside_string = true;
if ((char)s == '[' && !inside_string)
{
keybufferbuilder.Length = 0;
valuebufferbuilder.Length = 0;
reading = false;
inside_string = false;
reading_value = false;
}
if ((char)s == ',' && !inside_string && reading_number)
{
if (!dataTable.Columns.Contains(keybufferbuilder.ToString()))
dataTable.Columns.Add(keybufferbuilder.ToString(), typeof(string));
if (!outdict.ContainsKey(keybufferbuilder.ToString()))
outdict.Add(keybufferbuilder.ToString(), valuebufferbuilder.ToString());
keybufferbuilder.Length = 0;
valuebufferbuilder.Length = 0;
reading_value = false;
reading_number = false;
}
continue;
}
//if we reach end of the string
if (inside_string)
{
if ((char)s == '"')
{
inside_string = false;
s = bufferreader.Read();
if ((char)s == ':')
{
reading_value = true;
continue;
}
if (reading_value && (char)s == ',')
{
//put the key-value pair into dictionary
if(!dataTable.Columns.Contains(keybufferbuilder.ToString()))
dataTable.Columns.Add(keybufferbuilder.ToString(),typeof(string));
if (!outdict.ContainsKey(keybufferbuilder.ToString()))
outdict.Add(keybufferbuilder.ToString(), valuebufferbuilder.ToString());
keybufferbuilder.Length = 0;
valuebufferbuilder.Length = 0;
reading_value = false;
}
if (reading_value && (char)s == '}')
{
if (!dataTable.Columns.Contains(keybufferbuilder.ToString()))
dataTable.Columns.Add(keybufferbuilder.ToString(), typeof(string));
if (!outdict.ContainsKey(keybufferbuilder.ToString()))
outdict.Add(keybufferbuilder.ToString(), valuebufferbuilder.ToString());
ICollection key = outdict.Keys;
DataRow newrow = dataTable.NewRow();
foreach (string k_loopVariable in key)
{
CommonModule.LogTheMessage(outdict[k_loopVariable],"","","");
newrow[k_loopVariable] = outdict[k_loopVariable];
}
dataTable.Rows.Add(newrow);
CommonModule.LogTheMessage(dataTable.Rows.Count.ToString(), "", "row_count", "");
outdict.Clear();
keybufferbuilder.Length=0;
valuebufferbuilder.Length=0;
reading_value = false;
reading = false;
continue;
}
}
else
{
if (reading_value)
{
valuebufferbuilder.Append((char)s);
continue;
}
else
{
keybufferbuilder.Append((char)s);
continue;
}
}
}
else
{
switch ((char)s)
{
case ':':
reading_value = true;
break;
default:
if (reading_value)
{
valuebufferbuilder.Append((char)s);
}
else
{
keybufferbuilder.Append((char)s);
}
break;
}
}
}
}
return dataTable;
}
}