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);
}

很好用.

推荐答案

This should work...

JavaScriptSerializer ser = new JavaScriptSerializer();
var records = new ser.Deserialize<List<Record>>(jsonData);

public class Person
{
    public string Name;
    public int Age;
    public string Location;
}
public class Record
{
    public Person record;
}

Json相关问答推荐

使用jolt删除空对象

如何在Power BI中集成API和JSON数据后将数据转换为表?

如何使用表键名称GROUP_BY

kotlinx-serialization:如何将具有不同类型对象的JsonArray转换为同一个Class

过go 24小时内判断员事件的EventBridge事件模式

在Ansible中从json中提取特定数据

从 Inno Setup 中的 JSON 文件的每个对象中读取特定字符串

jq:来自嵌套 JSON 的映射

如何使用 React 从 NASA IMAGES API 中解构所需信息

JSON 模式实际用于什么目的?

如何使用 LINQ 在 C# 中重构对象?

Play Framework:如何序列化/反序列化与 JSON 的枚举

JSON RPC - 什么是id?

未捕获的类型错误:无法读取 null 的属性props

JSON对象中的JavaScript递归搜索

将 Objective-C 对象序列化和反序列化为 JSON

gson:将 null 视为空字符串

将 json 转换为 C# 数组?

如何在 Java 中将 YAML 转换为 JSON?

如何在 postgresql 9.3 中循环 JSON 数组