0

I am currently in the process of setting up a plugin for a game server i host and i have a whole bunch of items that have a "category" between 1 & 11 as shown in a small snippet below. I only ask this because there are over 200 items i will need to sort into different categories.

    "ammo.rifle.hv_0": {
      "shortname": "ammo.rifle.hv",
      "customIcon": null,
      "amount": 64,
      "skinId": 0,
      "isBp": false,
      "category": 9,
      "displayName": "HV 5.56 Rifle Ammo",
      "cost": 10000,
      "cooldown": 0
    },
    "radiationresisttea.pure_0": {
      "shortname": "radiationresisttea.pure",
      "customIcon": null,
      "amount": 1,
      "skinId": 0,
      "isBp": false,
      "category": 8,
      "displayName": "Pure Anti-Rad Tea",
      "cost": 10000,
      "cooldown": 0
    },
    "healingtea.pure_0": {
      "shortname": "healingtea.pure",
      "customIcon": null,
      "amount": 1,
      "skinId": 0,
      "isBp": false,
      "category": 8,
      "displayName": "Pure Healing Tea",
      "cost": 10000,
      "cooldown": 0
    },
    "ammo.rifle_0": {
      "shortname": "ammo.rifle",
      "customIcon": null,
      "amount": 64,
      "skinId": 0,
      "isBp": false,
      "category": 9,
      "displayName": "5.56 Rifle Ammo",
      "cost": 10000,
      "cooldown": 0
    },
    "blueberries_0": {
      "shortname": "blueberries",
      "customIcon": null,
      "amount": 1,
      "skinId": 0,
      "isBp": false,
      "category": 8,
      "displayName": "Blueberries",
      "cost": 10000,
      "cooldown": 0
    }
}

From this is there a way to index them in a way it will select all the ones with a certain "category".

For example, If i want all the ones with "category": 9 it will display the following

    "ammo.rifle_0": {
      "shortname": "ammo.rifle",
      "customIcon": null,
      "amount": 64,
      "skinId": 0,
      "isBp": false,
      "category": 9,
      "displayName": "5.56 Rifle Ammo",
      "cost": 10000,
      "cooldown": 0
    },
    "ammo.rifle.hv_0": {
      "shortname": "ammo.rifle.hv",
      "customIcon": null,
      "amount": 64,
      "skinId": 0,
      "isBp": false,
      "category": 9,
      "displayName": "HV 5.56 Rifle Ammo",
      "cost": 10000,
      "cooldown": 0
    }

Please and thank you in advanced

1 Answers1

0

Well you could try this approach:

Define the corresponding class that matches your objects:

public class SomeObject{
  public string ShortName {get; set;}
  /// ... add all other properties.
}

Convert your incoming data from json to c# objects:

Here you could choose to use Newtonsoft package. But there's many other libraries available to help you out on this. But here's what you could do using Newtonsoft

var myData = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string,SomeObject> >(jsonString);

This will create a new Dictionary<string, SomeObject> based on the data provided.


You can query the data in myData using LINQ i.e Let's get the items where category == 9

result = myData.Where(p => p.Category == 9).ToDictionary<string,SomeObject>(); 

This will get a new dictionary as result with keys whose SomeObject.caterory equals to 9.


Some references that will help you include:

  1. Convert JSON to C#
  2. Query Dictionary using LINQ
Mosia Thabo
  • 4,009
  • 1
  • 14
  • 24