对象引用未在列表中的 Json 序列化中设置错误 [英] Object reference not set Error in Json Serialize in Lists

查看:48
本文介绍了对象引用未在列表中的 Json 序列化中设置错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 个 c# 类,其中有 2 个列表,当我尝试创建一个 json 字符串时,我的对象引用未设置为对象错误的实例.

I have 3 c# classes in that I have 2 lists, when I trying to make a json string, I got Object reference not set to an instance of an object Error.

我尝试过使用一个列表.它完全有效,但是我的程序中有 2 个或更多类的列表,它不起作用.请帮我解决.

I have tried with one list. it perfectly works, but 2 classes or more classes have the list in my program, it not worked. pls help me resolve.

模型 Json -

{
  "accessKey": "7eb228097576abf56968e9845ab51b90",
  "channelId": "103",
  "hotels": [
    {
      "hotelId": "2",
      "rooms": [
        {
          "roomId": "1"
        }
      ]
    }
  ]
}

C# 类 -

public class RootObject
{
    public string accessKey { get; set; }
    public string channelId { get; set; }
    public List<Hotel> hotels { get; set; }            
}
public class Hotel
{
    public string hotelId { get; set; }
    public List<Room> rooms { get; set; }           
}
public class Room
{
    public string roomId { get; set; }           
}

C#

公共字符串cc(){

    string s = "";
    RootObject ro = new RootObject();
    ro.accessKey = "7eb228097576abf56968e9845ab51b90";
    ro.channelId = "103";
    ro.hotels = new List<Hotel>();

    Hotel h = new Hotel();
    Room r = new Room();

    string config = "server=localhost;username=someuser;password=somepwd;database=db";

    MySqlConnection connection = new MySqlConnection(config);

    string query = "select * from test1";

    MySqlCommand command = new MySqlCommand(query, connection);
    connection.Open();
    MySqlDataReader Reader = command.ExecuteReader();
    while (Reader.Read())
    {
         r.roomId = Reader[2].ToString();
    }
    connection.Close();

    query = "select * from test1";

    command = new MySqlCommand(query, connection);
    connection.Open();
    Reader = command.ExecuteReader();
    while (Reader.Read())
    {
        h.hotelId = Reader[1].ToString();
    }
    connection.Close();

    h.rooms.Add(r);       // Object reference not set to an instance of an object Error

    ro.hotels.Add(h);


    JavaScriptSerializer js = new JavaScriptSerializer();
    s = js.Serialize(ro);

    return s;
    }

推荐答案

您需要初始化 Rooms 属性.将构造函数添加到执行此操作的 Hotel 类

You need to initialise the Rooms property. Add a constructor to the Hotel calss that does so

public class Hotel
{
    public Hotel(string hotelId){
         this.hotelId = hotelId;
         this.rooms = New List<Room>();
    }

    public string hotelId { get; private set; }
    public List<Room> rooms { get; private set; }           
}

然后你会实例化一个这样的房间 new Hotel(hotelId)

then you'd instantiate a room like this new Hotel(hotelId)

将不应更改的内容封装起来通常是个好主意.通常不应从对象外部更改键.例如.更改酒店 ID 并保留房间列表是否有意义?

It's generally a good idea to keep what should not be changed encapsulated. Keys should usually not be changed from outside of the object. E.g. would it make sense to change the hotelId and keep the list of rooms?

建议根本不要公开集合的 setter.通常暴露一个getter就足够了,通常只暴露所需的操作会更好.在这种情况下公开一个 AddRoom 操作而不是公开房间列表

It's recommended not to expose a setter for collections at all. Usually it's enough to expose a getter and often it's even better to expose only the required operations E.g. in this case to expose a AddRoom operation rather than exposing the list of rooms

在您的代码中,您将遍历数据集中的所有记录(并且您这样做了两次)并覆盖了 hotelId 的值.即,您仅将数据集中的最后一条记录用于 hotelId 和 room

In your code you are looping over all records in a data set (and you're doing this twice) and are overriding the value for hotelId. Ie you are only using the last record in the data set both for hotelId and for room

这篇关于对象引用未在列表中的 Json 序列化中设置错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆