C# newtonsoft json追加多个json并创建一个新的统一json

2nc8po8w  于 2023-07-01  发布在  C#
关注(0)|答案(2)|浏览(206)

在文件路径中,我有几个json文件,我试图使用C# newtonsoft json将它们合并到一个文件中。
JSON1:

[{
        "name": "name1",
        "salary": 65000
    }, {
        "name": "name2",
        "salary": 68000
    }
]

JSON2:

[{
        "name": "name3",
        "salary": 56000
    }, {
        "name": "name4",
        "salary": 58000
    }
]

JSON3:

[{
        "name": "name5",
        "salary": 76000
    }, {
        "name": "name6",
        "salary": 78000
    }
]

JSON4:

[{
        "name": "name7",
        "salary": 86000
    }, {
        "name": "name8",
        "salary": 88000
    }
]

当我在C#中使用下面的代码时,我得到了如下所示的结果文件。

代码:

// List to store all the json objects
        List<object> combinedJsonObjects = new List<object>();

        // Load and append the JSON files
        foreach (string filePath in jsonFilePaths)
        {
            string jsonContent = File.ReadAllText(filePath);
            object jsonObject = JsonConvert.DeserializeObject(jsonContent);
            combinedJsonObjects.Add(jsonObject);
        }

        // Serialize the appended JSON objects
        string combinedJson = JsonConvert.SerializeObject(combinedJsonObjects, Newtonsoft.Json.Formatting.Indented);

        // save JSON file
        string combinedJsonFilePath = @"C:\filePath\new.json";
        File.WriteAllText(combinedJsonFilePath, combinedJson);

结果:

[
    [{
            "name": "name1",
            "salary": 65000
        }, {
            "name": "name2",
            "salary": 68000
        }
    ],
    [{
            "name": "name3",
            "salary": 56000
        }, {
            "name": "name4",
            "salary": 58000
        }
    ],
    [{
            "name": "name5",
            "salary": 76000
        }, {
            "name": "name6",
            "salary": 78000
        }
    ],
    [{
            "name": "name7",
            "salary": 86000
        }, {
            "name": "name8",
            "salary": 88000
        }
    ]
]

但是,当多个json文件被合并时,我希望文件看起来像下面这样。

  • 预期结果:*
[{
        "name": "name1",
        "salary": 65000
    }, {
        "name": "name2",
        "salary": 68000
    }, {
        "name": "name3",
        "salary": 56000
    }, {
        "name": "name4",
        "salary": 58000
    }, {
        "name": "name5",
        "salary": 76000
    }, {
        "name": "name6",
        "salary": 78000
    }, {
        "name": "name7",
        "salary": 86000
    }, {
        "name": "name8",
        "salary": 88000
    }
]
lnlaulya

lnlaulya1#

尝试使用JObject的集合:

List<JObject> combinedJsonObjects = new List<JObject>();

// Load and append the JSON files
foreach (string filePath in jsonFilePaths)
{
    string jsonContent = File.ReadAllText(filePath);
    object jsonObject = JsonConvert.DeserializeObject<JObject[]>(jsonContent);
    combinedJsonObjects.AddRange(jsonObject);
}

// Serialize the appended JSON objects
string combinedJson = JsonConvert.SerializeObject(combinedJsonObjects, Newtonsoft.Json.Formatting.Indented);

// save JSON file
string combinedJsonFilePath = @"C:\filePath\new.json";
File.WriteAllText(combinedJsonFilePath, combinedJson);

否则,序列化程序将整个JSON树作为结果集合的新元素进行处理(如结果输出中所示)。

dpiehjr4

dpiehjr42#

@GuruStron的答案是正确的。
另一种方法是读取每个JSON文件并将其转换为JArrayJArray.Parse(jsonContent)),然后通过JArray.Merge()JArray合并成一个数组。

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

List<object> combinedJsonObjects = new List<object>();
JArray jArray = new JArray();

// Load and append the JSON files
foreach (string filePath in jsonFilePaths)
{
    string jsonContent = File.ReadAllText(filePath);
    jArray.Merge(JArray.Parse(jsonContent));
}

// Serialize the appended JSON objects
string combinedJson = JsonConvert.SerializeObject(jArray, Newtonsoft.Json.Formatting.Indented);

// save JSON file
string combinedJsonFilePath = @"C:\filePath\new.json";
File.WriteAllText(combinedJsonFilePath, combinedJson);

相关问题