使用Class引用导出JSON文件 [英] Exporting JSON file with Class reference

查看:58
本文介绍了使用Class引用导出JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有这样的JSON文件。

Hi I have a JSON file like this.

{

  "FlightSchedule":[

    {

      "Id":"F110","
      "FlightType":"A380","
      " FlightDetailsFrom":" Melbourne",&
      "FlightDetailsTo":"Singapore","
      "日期":"20/3/2018","
      "价格":"$ 320",

      "容量":500,

      "预订":400,

      "可用":真实,

    },

{
  "FlightSchedule": [
    {
      "Id": "F110",
      "FlightType": "A380",
      "FlightDetailsFrom": "Melbourne",
      "FlightDetailsTo": "Singapore",
      "Date": "20/3/2018",
      "Price": "$320",
      "Capacity": 500,
      "Booked": 400,
      "Available": true,
    },

在其中导入此数据时,导入到类型为"对象"的"对象已保存","已存储"和"已存档"。包含"FlightSchedule"列表

When importing this data in it import to a Object of Type "Flights StoredFlights", "Flights" contains the List of "FlightSchedule"

现在我正在尝试写入JSON文件以通过新预订更新信息。

Now I am trying to Write to the JSON file to update the information with new bookings.

当我尝试导出我正在使用此代码的文件。

When I try and export the file I am using this code.

string json = JsonConvert.SerializeObject(StoredFlights.FlightSchedule.ToArray());

            System.IO.File.WriteAllText(" json1.json",json);

string json = JsonConvert.SerializeObject(StoredFlights.FlightSchedule.ToArray());
            System.IO.File.WriteAllText("json1.json", json);

但是这会以这种格式重写我的JSON文件,并且在开始时不会显示类引用,我该如何解决这个问题?

However this rewrites my JSON file in this format and doesn't show the class reference at the start, how do I fix this?







推荐答案

我认为你应该序列化这个成员的父级。或者试试这个:

string json = JsonConvert.SerializeObject( new { FlightSchedule = StoredFlights.FlightSchedule } );





这篇关于使用Class引用导出JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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