I'm stuck with a tricky problem.
我有一个JSON字符串,格式如下:
[{
"record":
{
"Name": "Komal",
"Age": 24,
"Location": "Siliguri"
}
},
{
"record":
{
"Name": "Koena",
"Age": 27,
"Location": "Barasat"
}
},
{
"record":
{
"Name": "Kanan",
"Age": 35,
"Location": "Uttarpara"
}
}
... ...
]
"记录"中的字段可以增加或减少.
So, I've made classes like this:
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);
I'm doing something wrong. But unable to find. Can you please help.
提前谢谢.
Update:
实际上,我得到了错误"无效的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);
}
很好用.