我不熟悉使用JSON数据。
我正在从网络服务中读取数据。发回的查询数据如下:
[["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数组的查询数据,其中第一行提供列名,后续行提供数据值。
答案 0 :(得分:45)
您可以非常轻松地反序列化。 C#中的数据结构只是List<string[]>
所以你可以这么做;
List<string[]> data = JsonConvert.DeserializeObject<List<string[]>>(jsonString);
上面的代码假设你正在使用json.NET。
编辑:注意json在技术上是一个字符串数组的数组。我更喜欢使用List<string[]>
作为我自己的声明,因为它更直观。它不会对json.NET造成任何问题,如果你想让它成为一个字符串数组的数组,那么你需要将类型更改为(我认为)string[][]
但是有一些有趣的小问题与锯齿状和我不太了解C#中的2D数组,所以我不打算在这里处理它。
答案 1 :(得分:31)
如果你使用.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);
答案 2 :(得分:0)
步骤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);
}
}
答案 3 :(得分:0)
您可以编写自己的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;
}
}