我有一个如下格式的JSON字符串:
[{
"record":
{
"Name": "Komal",
"Age": 24,
"Location": "Siliguri"
}
},
{
"record":
{
"Name": "Koena",
"Age": 27,
"Location": "Barasat"
}
},
{
"record":
{
"Name": "Kanan",
"Age": 35,
"Location": "Uttarpara"
}
}
... ...
]
"记录"中的字段可以增加或减少。
所以,我编了这样的类:
public class Person
{
public string Name;
public string Age;
}
public class PersonList
{
public Person record;
}
尝试反序列化如下:
JavaScriptSerializer ser = new JavaScriptSerializer();
var r = ser.Deserialize<PersonList>(jsonData);
我做错了什么。但是找不到。你能帮忙吗?
- 更新日期:**
实际上,我得到错误"无效的JSON原语:.",因为我在读取包含以下代码的文件时获得了字符串:
public static bool ReadFromFile(string path, string fileName, out string readContent)
{
bool status = true;
byte[] readBuffer = null;
try
{
// Combine the new file name with the path
string filePath = System.IO.Path.Combine(path, fileName);
readBuffer = System.IO.File.ReadAllBytes(filePath);
}
catch (Exception ex)
{
status = false;
}
readContent = (null != readBuffer) ? Utilities.GetString(readBuffer) : string.Empty;
return status;
}
现在我读的文件是这样的:
using (StreamReader r = new StreamReader("E:\\Work\\Data.json"))
{
string json = r.ReadToEnd();
result = JsonConvert.DeserializeObject<List<PersonList>>(json);
}
一切正常
3条答案
按热度按时间23c0lvtd1#
应该可以...
nsc4cvqm2#
这个代码对我来说很好用
4nkexdtk3#
并删除“{"..,
反序列化对象..,