C#在不知道它们的情况下获取json名称

C#在不知道它们的情况下获取json名称

本文介绍了C#在不知道它们的情况下获取json名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个Json:

  {
objects:{
realms / lang /de_DE.lang:{
hash:729b2c09d5c588787b23127eeda2730f9c039194,
size:7784
},
realms / lang / cy_GB.lang:{
hash:7b52463b2df4685d2d82c5d257fd5ec79843d618,
size:7688
},
minecraft / sounds / mob / chicken / step2.ogg:{
hash:bf7fadaf64945f6b31c803d086ac6a652aabef9b,
size:3838
},
minecraft / sounds / dig / gravel3.ogg:{
hash:48f7e1bb098abd36b9760cca27b9d4391a23de26 ,
size:6905
},
minecraft / lang / uk_UA.lang:{
hash:02ca127554fb1294b473bac256469a0d908ecd86,
size :97299
},
minecraft / sounds / mob / cat / hitt3.ogg:{
hash:3d98b4b7c37a09101da00d8a3078e0acba6558f4,
size:7371
},
minecraft / sounds / mob / horse / zombie / hit3.ogg:{
has h:ad126e435cca94054bf0d616301799a105526cde,
size:7043
},
minecraft / sounds / mob / endermen / portal.ogg:{
hash: 7b4b5323ef066caa1ae43cbe66fffd9dfce4ed32,
size:10010
},
minecraft / sounds / random / glass2.ogg:{
hash:87c47bda3645c68f18a49e83cbf06e5302d087ff,
size:7532
},
minecraft / sounds / mob / bat / hurt1.ogg:{
hash:fddba78b2426f8056be081c98fe82e8411a0a5cf,
size :4878
},
minecraft / sounds / ambient / cave / cave9.ogg:{
hash:b463fa47816fe9a5dfe508093150e647403e4db6,
size:27096
$,
minecraft / sounds / records / 13.ogg:{
hash:9cbed0e40296f68c61090c9b81816061e068a0ec,
size:1071028
},
minecraft / sounds / mob / enderdragon / wings1.ogg:{
hash:5a368ed32b3357e31629451ace57eb30d90e8874,
size:10617
},
minecraft / lang / ast_ES.lang:{
hash:fc2139572f55c973aedc694b8d633f2a4eb0907a,
size:72927
},
realms / lang / ast_ES.lang :{
hash:3f0a1aea8af9451493927ecb8a18997881ad0e2f,
size:7608
},
minecraft / sounds / mob / cow / step3.ogg:{
hash:482919bc631b317422ab44af3938259bea73fe83,
size:7923
},
minecraft / sounds / step / cloth3.ogg:{
hash :9c63f2a3681832dc32d206f6830360bfe94b5bfc,
size:48​​97
},
minecraft / sounds / mob / endermen / hit2.ogg:{
hash:cf60aefd0b742c3fa15e808c1a0c33aebd534cc5 ,
size:6491
},
minecraft / sounds / random / eat3.ogg:{
hash:9f2c4bab5ed55f1714fffa223985c81babc8f6c0,
size:5813
},
minecraft / sounds / step / sand5.ogg:{
hash:9fd6d2c633d276b952f2ff2aaa1fa7e5fb5efd2a,
size:5182
$,
minecraft / sounds / step / ladder5.ogg:{
hash:e9a1caaddcde79cdac7aa8eabde50d4cfd1bc578,
size:6077
},
minecraft / sounds / music / game / nuance1.ogg:{
hash:cc1a80b6becfc6d6489c069f053d0a8adc71c339,
size:156322
},
/lang/zh_TW.lang:{
hash:d2f3c9c340a5cfe30fb7e333c81a4d853d9bbddd,
size:68697
},
minecraft / sounds / random / eat1.ogg :{
hash:dfee39084c771182f6b9e7cfb8c8bc4e255747bc,
size:5678
},
minecraft / icons / minecraft.icns:{
hash:991b421dfd401f115241601b2b373140a8d78572,
size:114786
},
minecraft / lang / es_AR.lang:{
hash:e55edae0011215873faeba154c7d89f590ddb748,
size:74392
},
minecraft / sounds / mob / blaze / breathe4.ogg:{
hash:78d544a240d627005aaef6033fd646eafc66fe7a,
size:22054
},
minecraft / sounds / dig / sand4.ogg:{
hash:37afa06f97d58767a1cd1382386db878be1532dd,
size :5491
}
}
}

这是我的代码:

 列表< string> namesList = new List< string>(); 
dynamic json1 = JsonConvert.DeserializeObject(data);

现在我卡住了...
我有两个问题:我必须阅读对象中的每个名字都像


  realms / lang / de_DE.lang 

问题是,我不知道名称和Internet上我只是发现如何读取动态json对象中的值,而不是如何读取名称它... ...

第二个问题是,在另一个类中,我必须处理所有这些数据,我也需要哈希值。计算每个文件的时刻(有675个文件)或将哈希值作为返回值计算散列值是否方便?
我已经返回一个列表转换为一个URL数组,所以我不知道如何返回URL和HASH,然后处理它们。
我使用foreach循环向名称列表中添加值:

  namesList.Add(finalurl); 

然后我返回这个:

  return namesList.ToArray(); 


解决方案

您应该像下面这样为反序列化创建类: p>

  public class MyClass 
{
[JsonProperty(objects)]
public IDictionary< string ,ObjectValue> Objects {get;组; }

$ b $ public class ObjectValue
{
[JsonProperty(hash)]
public string Hash {get;组; }

[JsonProperty(size)]
public long Size {get;组; }
}

然后你可以像这样得到你的名字:

  MyClass json = JsonConvert.DeserializeObject< MyClass>(data); 
var names = json.Objects.Keys.ToList();
var hash = json.Objects [names [0]]。 //获取第一项的散列值

另外不要忘记检查 null 和其他可能的错误(没有数据等)。


I have this Json:

{
  "objects": {
    "realms/lang/de_DE.lang": {
      "hash": "729b2c09d5c588787b23127eeda2730f9c039194",
      "size": 7784
    },
    "realms/lang/cy_GB.lang": {
      "hash": "7b52463b2df4685d2d82c5d257fd5ec79843d618",
      "size": 7688
    },
    "minecraft/sounds/mob/chicken/step2.ogg": {
      "hash": "bf7fadaf64945f6b31c803d086ac6a652aabef9b",
      "size": 3838
    },
    "minecraft/sounds/dig/gravel3.ogg": {
      "hash": "48f7e1bb098abd36b9760cca27b9d4391a23de26",
      "size": 6905
    },
    "minecraft/lang/uk_UA.lang": {
      "hash": "02ca127554fb1294b473bac256469a0d908ecd86",
      "size": 97299
    },
    "minecraft/sounds/mob/cat/hitt3.ogg": {
      "hash": "3d98b4b7c37a09101da00d8a3078e0acba6558f4",
      "size": 7371
    },
    "minecraft/sounds/mob/horse/zombie/hit3.ogg": {
      "hash": "ad126e435cca94054bf0d616301799a105526cde",
      "size": 7043
    },
    "minecraft/sounds/mob/endermen/portal.ogg": {
      "hash": "7b4b5323ef066caa1ae43cbe66fffd9dfce4ed32",
      "size": 10010
    },
    "minecraft/sounds/random/glass2.ogg": {
      "hash": "87c47bda3645c68f18a49e83cbf06e5302d087ff",
      "size": 7532
    },
    "minecraft/sounds/mob/bat/hurt1.ogg": {
      "hash": "fddba78b2426f8056be081c98fe82e8411a0a5cf",
      "size": 4878
    },
    "minecraft/sounds/ambient/cave/cave9.ogg": {
      "hash": "b463fa47816fe9a5dfe508093150e647403e4db6",
      "size": 27096
    },
    "minecraft/sounds/records/13.ogg": {
      "hash": "9cbed0e40296f68c61090c9b81816061e068a0ec",
      "size": 1071028
    },
    "minecraft/sounds/mob/enderdragon/wings1.ogg": {
      "hash": "5a368ed32b3357e31629451ace57eb30d90e8874",
      "size": 10617
    },
    "minecraft/lang/ast_ES.lang": {
      "hash": "fc2139572f55c973aedc694b8d633f2a4eb0907a",
      "size": 72927
    },
    "realms/lang/ast_ES.lang": {
      "hash": "3f0a1aea8af9451493927ecb8a18997881ad0e2f",
      "size": 7608
    },
    "minecraft/sounds/mob/cow/step3.ogg": {
      "hash": "482919bc631b317422ab44af3938259bea73fe83",
      "size": 7923
    },
    "minecraft/sounds/step/cloth3.ogg": {
      "hash": "9c63f2a3681832dc32d206f6830360bfe94b5bfc",
      "size": 4897
    },
    "minecraft/sounds/mob/endermen/hit2.ogg": {
      "hash": "cf60aefd0b742c3fa15e808c1a0c33aebd534cc5",
      "size": 6491
    },
    "minecraft/sounds/random/eat3.ogg": {
      "hash": "9f2c4bab5ed55f1714fffa223985c81babc8f6c0",
      "size": 5813
    },
    "minecraft/sounds/step/sand5.ogg": {
      "hash": "9fd6d2c633d276b952f2ff2aaa1fa7e5fb5efd2a",
      "size": 5182
    },
    "minecraft/sounds/step/ladder5.ogg": {
      "hash": "e9a1caaddcde79cdac7aa8eabde50d4cfd1bc578",
      "size": 6077
    },
    "minecraft/sounds/music/game/nuance1.ogg": {
      "hash": "cc1a80b6becfc6d6489c069f053d0a8adc71c339",
      "size": 156322
    },
    "minecraft/lang/zh_TW.lang": {
      "hash": "d2f3c9c340a5cfe30fb7e333c81a4d853d9bbddd",
      "size": 68697
    },
    "minecraft/sounds/random/eat1.ogg": {
      "hash": "dfee39084c771182f6b9e7cfb8c8bc4e255747bc",
      "size": 5678
    },
    "minecraft/icons/minecraft.icns": {
      "hash": "991b421dfd401f115241601b2b373140a8d78572",
      "size": 114786
    },
    "minecraft/lang/es_AR.lang": {
      "hash": "e55edae0011215873faeba154c7d89f590ddb748",
      "size": 74392
    },
    "minecraft/sounds/mob/blaze/breathe4.ogg": {
      "hash": "78d544a240d627005aaef6033fd646eafc66fe7a",
      "size": 22054
    },
    "minecraft/sounds/dig/sand4.ogg": {
      "hash": "37afa06f97d58767a1cd1382386db878be1532dd",
      "size": 5491
    }
  }
}

Here is my code:

    List<string> namesList = new List<string>();
    dynamic json1 = JsonConvert.DeserializeObject(data);

Now I'm stuck...I have two problems: I have to read every name inside objects like

realms/lang/de_DE.lang

The problem is that i don't know the names and on the internet i just found how to read the values inside dynamic json objects and not how to read the name of it...

The second problem is that on another class i have to process all this data and i need the hashes too. Is it convenient to calculate the hash on the moment for every file(there are 675 files) or to pass the hashes as a return?I already return a list converted to an array of URL's so i don't know how to return both the URL's and the HASH and then process them.I add to the namelist the value with a foreach loop:

namesList.Add(finalurl);

And then i return this:

return namesList.ToArray();
解决方案

You should create classes for deserialization like this:

public class MyClass
{
    [JsonProperty("objects")]
    public IDictionary<string, ObjectValue> Objects { get; set; }
}

public class ObjectValue
{
    [JsonProperty("hash")]
    public string Hash { get; set; }

    [JsonProperty("size")]
    public long Size { get; set; }
}

and then you can get your names like this:

MyClass json = JsonConvert.DeserializeObject<MyClass>(data);
var names = json.Objects.Keys.ToList();
var hash = json.Objects[names[0]].Hash; // Get the hash of the first item

Also don't forget to check if null and other possible errors (no data etc).

这篇关于C#在不知道它们的情况下获取json名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 20:49