首页 > 解决方案 > 如何将一堆 json 文件合并到一个大文件中?

问题描述

所以我在一个文件夹中有一堆 json 文件,我想将它们全部合并到一个大 json 文件中。

所以我知道你可以做到这一点,但我不确定如何将它添加到循环中,所以它会一直添加到 1。

var jObject1 = // Your first json object as JObject
var jObject2 = // Your second json object as JObject 

jObject1.Merge(jObject2);

这是一些示例数据 https://hatebin.com/iuqscvgmqk

我已经制作了类来建模对象

但我面临的问题是我不知道如何将它们全部合并到一个大文件中,我想使用一个 foreach,有点像这样

foreach (var file in Directory.GetFiles("Path"))
            {

            }


 public class Requirements
    {
        public int ranged { get; set; }
    }

    public class Equipment
    {
        public int attack_stab { get; set; }
        public int attack_slash { get; set; }
        public int attack_crush { get; set; }
        public int attack_magic { get; set; }
        public int attack_ranged { get; set; }
        public int defence_stab { get; set; }
        public int defence_slash { get; set; }
        public int defence_crush { get; set; }
        public int defence_magic { get; set; }
        public int defence_ranged { get; set; }
        public int melee_strength { get; set; }
        public int ranged_strength { get; set; }
        public int magic_damage { get; set; }
        public int prayer { get; set; }
        public string slot { get; set; }
        public Requirements requirements { get; set; }
    }

    public class Stance
    {
        public string combat_style { get; set; }
        public object attack_type { get; set; }
        public object attack_style { get; set; }
        public string experience { get; set; }
        public string boosts { get; set; }
    }

    public class Weapon
    {
        public int attack_speed { get; set; }
        public string weapon_type { get; set; }
        public List<Stance> stances { get; set; }
    }

    public class Item
    {
        public int id { get; set; }
        public string name { get; set; }
        public bool incomplete { get; set; }
        public bool members { get; set; }
        public bool tradeable { get; set; }
        public bool tradeable_on_ge { get; set; }
        public bool stackable { get; set; }
        public bool noted { get; set; }
        public bool noteable { get; set; }
        public object linked_id_item { get; set; }
        public int linked_id_noted { get; set; }
        public int linked_id_placeholder { get; set; }
        public bool placeholder { get; set; }
        public bool equipable { get; set; }
        public bool equipable_by_player { get; set; }
        public bool equipable_weapon { get; set; }
        public int cost { get; set; }
        public int lowalch { get; set; }
        public int highalch { get; set; }
        public double weight { get; set; }
        public int buy_limit { get; set; }
        public bool quest_item { get; set; }
        public string release_date { get; set; }
        public bool duplicate { get; set; }
        public string examine { get; set; }
        public string icon { get; set; }
        public string wiki_name { get; set; }
        public string wiki_url { get; set; }
        public Equipment equipment { get; set; }
        public Weapon weapon { get; set; }
    }

标签: c#.net.net-core

解决方案


如果您尝试将它们组合为 json,则需要一个数组。所以你会像这样组合这些对象:

    var ja = new JArray();
    ja.Add(jObject1);
    ja.Add(jObject2);

如果这是您的最终目标,您当然可以稍后转换为您的 .NET 对象。


推荐阅读