Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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
C# JSON数据在压缩过程中丢失_C#_Json_Zip_Json.net - Fatal编程技术网

C# JSON数据在压缩过程中丢失

C# JSON数据在压缩过程中丢失,c#,json,zip,json.net,C#,Json,Zip,Json.net,我有一个非常奇怪的问题。我有一堆我正试图以JSON格式存储的对象。包含JSON的文件将与项目引用的任何资源一起放在zip存档中。当我将对象序列化为JSON时,这很好,但是一旦文件被归档然后被提取,一些字段就会消失 我的代码: string root = Path.GetTempPath() + "package_root\\"; string contents = JsonConvert.SerializeObject(package, Formatting.Indented); File.W

我有一个非常奇怪的问题。我有一堆我正试图以JSON格式存储的对象。包含JSON的文件将与项目引用的任何资源一起放在zip存档中。当我将对象序列化为JSON时,这很好,但是一旦文件被归档然后被提取,一些字段就会消失

我的代码:

string root = Path.GetTempPath() + "package_root\\";

string contents = JsonConvert.SerializeObject(package, Formatting.Indented);
File.WriteAllText(root + "contents.json", contents);

byte[] buf;

 _ParseResource(root, package, package.BackgroundImage);
foreach (var resource in package.Resources)
{
    _ParseResource(root, package, resource);
}

if (package.PackagePath == path)
{
    package.Archive.Dispose();
    package.Archive = null;
}

ZipFile.CreateFromDirectory(root, outputPath, CompressionLevel.NoCompression, false);
压缩前的JSON:

{
    "Nodes":[
    {
        "NodeID": 0,
        "NodeType": 1,
        "Position": "0.0537248952329852,0.460410557184751",
        "ActionType": "SimpleTextImageAction",
        "Metadata": {
            "ImageID": -1,
            "Text": "",
            "MetadataTypeID": 2,
            "Tooltip": null
        }
    }
    ],
    "Resources":[],
    "PackageVersion": 1.0,
    "BackgroundImage": {
        "ResourceID": -1,
        "Path": "C:\\Users\\Abion47-SSD\\Desktop\\Panorama Images\\89_-_Machu_Picchu_-_Juin_2009.jpg",
        "Type": 1,
        "CacheOnLoad": true,
        "Origin": 2
    }
}
压缩后的JSON:

{  
    "Nodes":[  
    {  
        "NodeType":1,
        "Position":"0.0801201698474518,0.294721407624633",
        "ActionType":"SimpleTextAction",
        "Metadata":{  
            "Text":"Tooltip",
            "Tooltip":null
        }
    }
    ],
    "Resources":[],
    "PackageVersion":1.0,
    "BackgroundImage":{  
        "ResourceID":-1,
        "Path":"C:\\Users\\Abion47\\Desktop\\Panorama Files\\89_-_Machu_Picchu_-_Juin_2009.jpg",
        "Type":1,
        "CacheOnLoad":true,
        "Origin":2
    }
}

我无法重现你的问题。下面是我用来(尝试)复制它的代码:

string root = Path.GetTempPath() + "package_root\\";

string contents = 
@"{
""Nodes"":[
{
    ""NodeID"": 0,
    ""NodeType"": 1,
    ""Position"": ""0.0537248952329852,0.460410557184751"",
    ""ActionType"": ""SimpleTextImageAction"",
    ""Metadata"": {
        ""ImageID"": -1,
        ""Text"": """",
        ""MetadataTypeID"": 2,
        ""Tooltip"": null
    }
}
],
""Resources"":[],
""PackageVersion"": 1.0,
""BackgroundImage"": {
    ""ResourceID"": -1,
    ""Path"": ""C:\\Users\\Abion47-SSD\\Desktop\\Panorama Images\\89_-_Machu_Picchu_-_Juin_2009.jpg"",
    ""Type"": 1,
    ""CacheOnLoad"": true,
    ""Origin"": 2
}
}";

File.WriteAllText(Path.Combine(root + "contents.json"), contents);

/* SNIP resource inclusion */

if (!Directory.Exists(root))
    Directory.CreateDirectory(root);

var outDir = Path.Combine(Path.GetTempPath(), "outDir");
if (!Directory.Exists(outDir))
    Directory.CreateDirectory(outDir);

var outZipFullPath = Path.Combine(outDir, "out.zip");
if (File.Exists(outZipFullPath))
    File.Delete(outZipFullPath);

ZipFile.CreateFromDirectory(root, outZipFullPath, CompressionLevel.NoCompression, false);

var outContentsFile = Path.Combine(outDir, "contents.json");

if (File.Exists(outContentsFile))
    File.Delete(outContentsFile);

ZipFile.Open(outZipFullPath, ZipArchiveMode.Read).Entries.First().ExtractToFile(outContentsFile);

Console.WriteLine(File.ReadAllText(outContentsFile) == contents);

为我打印
true
。很可能是您打开并比较了错误的zip文件

Hi Abion,请提供您用于压缩和解压缩的名称、版本和库,好吗?如果可能的话,您是否也可以将代码编辑成简单的
var content=[您的json];Zip();解压缩()?这将使1)每个人更容易诊断实际问题,2)让答案更容易复制并粘贴代码来复制问题。我无法在我的机器上复制问题-它返回相同的json。你确定你打开的是你刚写的同一个档案吗?是的,我发现我在一个不同但名称非常相似的文件夹中。