Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Swift-iOS应用程序中使用Protobuf和JSON文件无法将JSON转换为Struct_Json_Swift_Protocol Buffers_Swiftprotobuf - Fatal编程技术网

在Swift-iOS应用程序中使用Protobuf和JSON文件无法将JSON转换为Struct

在Swift-iOS应用程序中使用Protobuf和JSON文件无法将JSON转换为Struct,json,swift,protocol-buffers,swiftprotobuf,Json,Swift,Protocol Buffers,Swiftprotobuf,我有一个json数据的.json文件(见下文)。我正在使用显示的代码从应用程序的本地文件加载数据 我希望了解我的json文件是否格式不正确,或者是否需要更改.Proto文件中的某些内容,或者代码是否不正确 我从serializedData try/catch中获得以下格式错误的Protobuf错误 致命错误:无法加载。来自主捆绑包的json.json:malformedProtobuf:file MyToyBox/ModelData.swift。来自主捆绑包的json:malformedProt

我有一个json数据的.json文件(见下文)。我正在使用显示的代码从应用程序的本地文件加载数据

我希望了解我的json文件是否格式不正确,或者是否需要更改.Proto文件中的某些内容,或者代码是否不正确

我从serializedData try/catch中获得以下格式错误的Protobuf错误

致命错误:无法加载。来自主捆绑包的json.json:malformedProtobuf:file MyToyBox/ModelData.swift。来自主捆绑包的json:malformedProtobuf:file MyToyBox/ModelData.swift,第563行

从文件中
///二进制数据在某些方面存在格式错误,例如无效的导线格式///或字段标记。

    let data: Data

    do {
        data = try Data(contentsOf: fileURL)
    } catch {
        fatalError("Couldn't load \(fileURL.absoluteString) from main bundle:\n\(error)")
    }
    
    do {
        let response = try SeriesInfo(serializedData: data)
        
        guard let receivedFromJSON = try? SeriesInfo(jsonUTF8Data: response.jsonUTF8Data()) else{
            fatalError("Couldn't load \(fileURL.absoluteString) from main bundle:\n")
        }
        
        return receivedFromJSON
    }
    catch {
        fatalError("Couldn't load \(fileURL.absoluteString) from main bundle:\n\(error)")
    }
.Proto文件

syntax = "proto3";

message SeriesInfo {
    message SeriesUnit {
    string Series_Unique_ID = 1;
    string Series_Record_Name = 2;
    string Series_Name = 3;
    int32 Series_From_Year = 4;
    optional int32 Series_To_Year = 5;
    int32 Is_Active = 6;
    string Logo_Image_Name = 7;
    int32 Sort_Order = 8;
    }
    repeated SeriesUnit seriesList = 1;
}
[
  {
    "Series_Unique_ID" : "SWV",
    "Series_Name" : "Vintage",
    "Is_Active" : 1,
    "Sort_Order" : 1,
    "Series_From_Year" : 1977,
    "Series_Record_Name" : "Kenner_Star Wars",
    "Logo_Image_Name" : "SWV_Logo",
    "Series_To_Year" : 1985
  },
  {
    "Series_Unique_ID" : "SWVDeluxe",
    "Series_Name" : "Vintage Deluxe",
    "Is_Active" : 1,
    "Sort_Order" : 2,
    "Series_From_Year" : 1977,
    "Series_Record_Name" : "Kenner_Star Wars",
    "Logo_Image_Name" : "SWVDeluxe_Logo",
    "Series_To_Year" : 1985
  },
  {
    "Series_Unique_ID" : "SWPOTFII",
    "Series_Name" : "Power of the Force (II)",
    "Is_Active" : 1,
    "Sort_Order" : 3,
    "Series_From_Year" : 1995,
    "Series_Record_Name" : "Kenner\/Hasbro_Star Wars",
    "Logo_Image_Name" : "SWPOTFII_Logo",
    "Series_To_Year" : 2000
  },
  {
    "Series_Unique_ID" : "SWEITPM",
    "Series_Name" : "Episode I - The Phantom Manace",
    "Is_Active" : 1,
    "Sort_Order" : 4,
    "Series_From_Year" : 1999,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWEITPM_Logo",
    "Series_To_Year" : 2000
  },
  {
    "Series_Unique_ID" : "SWPOTJ",
    "Series_Name" : "Power of the Jedi",
    "Is_Active" : 1,
    "Sort_Order" : 5,
    "Series_From_Year" : 2000,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWPOTJ_Logo",
    "Series_To_Year" : 2002
  },
  {
    "Series_Unique_ID" : "SWSWS",
    "Series_Name" : "Star Wars Saga",
    "Is_Active" : 1,
    "Sort_Order" : 6,
    "Series_From_Year" : 2002,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWSWS_Logo",
    "Series_To_Year" : 2004
  },
  {
    "Series_Unique_ID" : "SWCW",
    "Series_Name" : "Clone Wars",
    "Is_Active" : 1,
    "Sort_Order" : 7,
    "Series_From_Year" : 2003,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWCW_Logo",
    "Series_To_Year" : 2005
  },
  {
    "Series_Unique_ID" : "SWOTC",
    "Series_Name" : "The Original Trilogy Collection",
    "Is_Active" : 1,
    "Sort_Order" : 8,
    "Series_From_Year" : 2004,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWOTC_Logo",
    "Series_To_Year" : 2005
  },
  {
    "Series_Unique_ID" : "SWROTS",
    "Series_Name" : "Revenge of the Sith",
    "Is_Active" : 1,
    "Sort_Order" : 9,
    "Series_From_Year" : 2005,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWROTS_Logo",
    "Series_To_Year" : 2006
  },
  {
    "Series_Unique_ID" : "SWTSC",
    "Series_Name" : "The Saga Collection",
    "Is_Active" : 1,
    "Sort_Order" : 10,
    "Series_From_Year" : 2006,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWTSC_Logo",
    "Series_To_Year" : 2007
  },
  {
    "Series_Unique_ID" : "SW30AC",
    "Series_Name" : "30th Anniversary Collection",
    "Is_Active" : 1,
    "Sort_Order" : 11,
    "Series_From_Year" : 2007,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SW30AC_Logo",
    "Series_To_Year" : 2008
  },
  {
    "Series_Unique_ID" : "SWSL0708",
    "Series_Name" : "Saga Legends 2007-2008",
    "Is_Active" : 1,
    "Sort_Order" : 12,
    "Series_From_Year" : 2007,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWSL0708_Logo",
    "Series_To_Year" : 2008
  },
  {
    "Series_Unique_ID" : "SWTCW0809",
    "Series_Name" : "The Clone Wars 2008-2009",
    "Is_Active" : 1,
    "Sort_Order" : 13,
    "Series_From_Year" : 2008,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWTCW0809_Logo",
    "Series_To_Year" : 2009
  },
  {
    "Series_Unique_ID" : "SWTLC0809",
    "Series_Name" : "The Legacy Collection 2008-2009",
    "Is_Active" : 1,
    "Sort_Order" : 14,
    "Series_From_Year" : 2008,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWTLC0809_Logo",
    "Series_To_Year" : 2009
  },
  {
    "Series_Unique_ID" : "SWSL0808",
    "Series_Name" : "Saga Legends 2008",
    "Is_Active" : 1,
    "Sort_Order" : 15,
    "Series_From_Year" : 2008,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWSL0808_Logo",
    "Series_To_Year" : 2008
  },
  {
    "Series_Unique_ID" : "SWTCW0909",
    "Series_Name" : "The Clone Wars 2009",
    "Is_Active" : 1,
    "Sort_Order" : 16,
    "Series_From_Year" : 2009,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWTCW0909_Logo",
    "Series_To_Year" : 2009
  },
  {
    "Series_Unique_ID" : "SWTLC0910",
    "Series_Name" : "The Legacy Collection 2009-2010",
    "Is_Active" : 1,
    "Sort_Order" : 17,
    "Series_From_Year" : 2009,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWTLC0910_Logo",
    "Series_To_Year" : 2010
  },
  {
    "Series_Unique_ID" : "SWSL0909",
    "Series_Name" : "Saga Legends 2009",
    "Is_Active" : 1,
    "Sort_Order" : 18,
    "Series_From_Year" : 2009,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWSL0909_Logo",
    "Series_To_Year" : 2009
  },
  {
    "Series_Unique_ID" : "SWTCW1011",
    "Series_Name" : "The Clone Wars 2010-2011",
    "Is_Active" : 1,
    "Sort_Order" : 19,
    "Series_From_Year" : 2010,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWTCW1011_Logo",
    "Series_To_Year" : 2011
  },
  {
    "Series_Unique_ID" : "SWSL1011",
    "Series_Name" : "Saga Legends 2010-2011 [Shadow of the Dark Side]",
    "Is_Active" : 1,
    "Sort_Order" : 20,
    "Series_From_Year" : 2010,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWSL1011_Logo",
    "Series_To_Year" : 2011
  },
  {
    "Series_Unique_ID" : "SWTVC",
    "Series_Name" : "Star Wars: The Vintage Collection",
    "Is_Active" : 1,
    "Sort_Order" : 21,
    "Series_From_Year" : 2010,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWTVC_Logo",
    "Series_To_Year" : null
  },
  {
    "Series_Unique_ID" : "SWTCW1212",
    "Series_Name" : "The Clone Wars 2012",
    "Is_Active" : 1,
    "Sort_Order" : 22,
    "Series_From_Year" : 2012,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWTCW1212_Logo",
    "Series_To_Year" : 2012
  },
  {
    "Series_Unique_ID" : "SWDTF",
    "Series_Name" : "Discover the Force",
    "Is_Active" : 1,
    "Sort_Order" : 23,
    "Series_From_Year" : 2012,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWDTF_Logo",
    "Series_To_Year" : 2013
  },
  {
    "Series_Unique_ID" : "SWMH",
    "Series_Name" : "Movie Heroes",
    "Is_Active" : 1,
    "Sort_Order" : 24,
    "Series_From_Year" : 2012,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWMH_Logo",
    "Series_To_Year" : 2013
  },
  {
    "Series_Unique_ID" : "SWTCW1313",
    "Series_Name" : "The Clone Wars 2013",
    "Is_Active" : 1,
    "Sort_Order" : 25,
    "Series_From_Year" : 2013,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWTCW1313_Logo",
    "Series_To_Year" : 2013
  },
  {
    "Series_Unique_ID" : "SWSL13",
    "Series_Name" : "Saga Legends 2013",
    "Is_Active" : 1,
    "Sort_Order" : 26,
    "Series_From_Year" : 2013,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWSL13_Logo",
    "Series_To_Year" : 2014
  },
  {
    "Series_Unique_ID" : "SWTLC1315",
    "Series_Name" : "The Legacy Collection 2013-2015",
    "Is_Active" : 1,
    "Sort_Order" : 27,
    "Series_From_Year" : 2013,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWTLC1315_Logo",
    "Series_To_Year" : 2015
  },
  {
    "Series_Unique_ID" : "SWSL14",
    "Series_Name" : "Saga Legends 2014",
    "Is_Active" : 1,
    "Sort_Order" : 28,
    "Series_From_Year" : 2014,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWSL14_Logo",
    "Series_To_Year" : 2015
  },
  {
    "Series_Unique_ID" : "SWBS375I",
    "Series_Name" : "The Black Series 3 3\/4 Inch",
    "Is_Active" : 1,
    "Sort_Order" : 29,
    "Series_From_Year" : 2013,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWBS375I_Logo",
    "Series_To_Year" : 2016
  },
  {
    "Series_Unique_ID" : "SWBS6I",
    "Series_Name" : "The Black Series 6 Inch",
    "Is_Active" : 1,
    "Sort_Order" : 30,
    "Series_From_Year" : 2013,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWBS6I_Logo",
    "Series_To_Year" : null
  },
  {
    "Series_Unique_ID" : "SWTFA",
    "Series_Name" : "The Force Awakens",
    "Is_Active" : 1,
    "Sort_Order" : 31,
    "Series_From_Year" : 2015,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWTFA_Logo",
    "Series_To_Year" : 2016
  },
  {
    "Series_Unique_ID" : "SWDPDF",
    "Series_Name" : "Disney Parks",
    "Is_Active" : 1,
    "Sort_Order" : 32,
    "Series_From_Year" : 2015,
    "Series_Record_Name" : "Disney Park_Star Wars",
    "Logo_Image_Name" : "SWDPDF_Logo",
    "Series_To_Year" : null
  },
  {
    "Series_Unique_ID" : "SWRO",
    "Series_Name" : "Rogue One",
    "Is_Active" : 1,
    "Sort_Order" : 33,
    "Series_From_Year" : 2016,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWRO_Logo",
    "Series_To_Year" : 2017
  },
  {
    "Series_Unique_ID" : "SWTLJ",
    "Series_Name" : "Force Link - The Last Jedi, Solo: A Star Wars Story",
    "Is_Active" : 1,
    "Sort_Order" : 34,
    "Series_From_Year" : 2017,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWTLJ_Logo",
    "Series_To_Year" : 2019
  },
  {
    "Series_Unique_ID" : "SWGOA",
    "Series_Name" : "Galaxy of Adventures",
    "Is_Active" : 1,
    "Sort_Order" : 35,
    "Series_From_Year" : 2018,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWGOA_Logo",
    "Series_To_Year" : null
  },
  {
    "Series_Unique_ID" : "SWRESIST",
    "Series_Name" : "Star Wars Resistance",
    "Is_Active" : 1,
    "Sort_Order" : 36,
    "Series_From_Year" : 2019,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWRESIST_Logo",
    "Series_To_Year" : 2019
  },
  {
    "Series_Unique_ID" : "SWBSHR",
    "Series_Name" : "Black Series Hyper-Real",
    "Is_Active" : 1,
    "Sort_Order" : 37,
    "Series_From_Year" : 2019,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWBSHR_Logo",
    "Series_To_Year" : null
  },
  {
    "Series_Unique_ID" : "SWRC",
    "Series_Name" : "Retro Collection",
    "Is_Active" : 1,
    "Sort_Order" : 38,
    "Series_From_Year" : 2019,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWRC_Logo",
    "Series_To_Year" : null
  },
  {
    "Series_Unique_ID" : "SWSS",
    "Series_Name" : "Skywalker Saga",
    "Is_Active" : 1,
    "Sort_Order" : 39,
    "Series_From_Year" : 2019,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWSS_Logo",
    "Series_To_Year" : null
  },
  {
    "Series_Unique_ID" : "SWCTS",
    "Series_Name" : "Celebrate the Saga",
    "Is_Active" : 1,
    "Sort_Order" : 40,
    "Series_From_Year" : 2020,
    "Series_Record_Name" : "Hasbro_Star Wars",
    "Logo_Image_Name" : "SWCTS_Logo",
    "Series_To_Year" : null
  }
]