2

So I have a quick question. I have a very large json file that has around 20,000 json objects within an array, and each of those objects have 3 additional nested objects (separate from each other) within them. Example:

    [
  {
    "id": 0,
    "name": "Dwarf remains",
    "exchangeable": false,
    "members": true,
    "placeholder_id": 17851,
    "actions": {
      "inventory": {
        "fourthop": "Destroy"
      },
      "world": {
        "secondop": "Take"
      }
    }
  },

  {
    "id": 1,
    "name": "Toolkit",
    "exchangeable": false,
    "members": true,
    "placeholder_id": 17852,
    "actions": {
      "inventory": {
        "fourthop": "Destroy"
      },
      "world": {
        "secondop": "Take"
      }
    }
  },

  {
    "id": 2,
    "name": "Cannonball",
    "exchangeable": true,
    "members": true,
    "placeholder_id": 17853,
    "values": {
      "value": 5,
      "alchemy_high": 3,
      "alchemy_low": 2
    },
    "actions": {
      "inventory": {
        "fourthop": "Drop"
      },
      "world": {
        "secondop": "Take"
      }
    }
  },

  {
    "id": 3,
    "name": "Nulodion\u0027s notes",
    "exchangeable": false,
    "members": true,
    "placeholder_id": 17854,
    "actions": {
      "inventory": {
        "0": "Read",
        "fourthop": "Destroy"
      },
      "world": {
        "secondop": "Take"
      }
    }
  },

  {
    "id": 4,
    "name": "Ammo mould",
    "exchangeable": false,
    "members": true,
    "placeholder_id": 17855,
    "values": {
      "value": 5,
      "alchemy_high": 3,
      "alchemy_low": 2
    },
    "actions": {
      "inventory": {
        "fourthop": "Drop"
      },
      "world": {
        "secondop": "Take"
      }
    }
  },

  {
    "id": 5,
    "name": "Instruction manual",
    "exchangeable": false,
    "members": true,
    "placeholder_id": 17856,
    "values": {
      "value": 10,
      "alchemy_high": 6,
      "alchemy_low": 4
    },
    "actions": {
      "inventory": {
        "0": "Read",
        "fourthop": "Drop"
      },
      "world": {
        "secondop": "Take"
      }
    }
  },

  {
    "id": 6,
    "name": "Cannon base",
    "exchangeable": true,
    "members": true,
    "noted_id": 7,
    "placeholder_id": 17857,
    "values": {
      "value": 187500,
      "alchemy_high": 112500,
      "alchemy_low": 75000
    },
    "actions": {
      "inventory": {
        "0": "Set-up",
        "fourthop": "Drop"
      },
      "world": {
        "secondop": "Take"
      }
    }
  },

  {
    "id": 8,
    "name": "Cannon stand",
    "exchangeable": true,
    "members": true,
    "noted_id": 9,
    "placeholder_id": 17858,
    "values": {
      "value": 187500,
      "alchemy_high": 112500,
      "alchemy_low": 75000
    },
    "actions": {
      "inventory": {
        "fourthop": "Drop"
      },
      "world": {
        "secondop": "Take"
      }
    }
  }
]

Now if I only want to deserialize the root object of each item:

"id": 8,
"name": "Cannon stand",
"exchangeable": true,
"members": true,
"noted_id": 9,
"placeholder_id": 17858,

(This section) Would I still need to create the three other additional classes for the values, actions, and world objects nested within each item, or is there a way to simply use a class resembling only the root object without those 3 areas and still have it all deserialize properly?

Or even to simply just pull the item name and id# and place them in a list? I ask because I'd rather not go ahead and waste my time if it is not possible. I've read several other threads but can't seem to find a clear indication to this specific instance. Other threads and articles/tutorials I've read about deserializing nested json objects tend to be talking about a json strings that are either not an array, they only wish to grab a single element, or the nesting is done differently.

So is this possible? And if so could you simply give me a little pointer or a link to something that may explain this sort of idea. Doesn't have to be super in depth, just really need someone to point the direction so I can find it easier. I've been searching threads for hours now with no luck.

Also one additional question, what would be the best way to store the item name and id number (other than a database) so that a user may search the item name in a textbox/combobox with autocomplete and that once the item is selected it will use the corresponding id number to search that number in another api.

Is there any decent way of doing this or should I stick with my original thought of simply importing this json array into a dataset and then save it as xml and simply import the xml to the dataset on startup?

I'd rather avoid this simply so instead of users needing to constantly check for new versions of the xml file they can simply grab the up to date json every startup and make use of that. That way I only need to worry about changing the file on the api.

Note: I tried to make sure this was not a duplicate post and looked at several threads with no avail. However, I'm running on only a few hours sleep and it is entirely possible for me to have made a mistake. If I did, and this is a duplicate post, please let me know and I apologize in advance if it is. I tried my very best at the moment to make sure I didn't duplicate a thread.

6
  • 1
    Just omit the properties of the data you dont want from the target class(es) Commented Jan 22, 2018 at 17:06
  • Oh wow I didn't think that would actually work because it seem's so simple.. And that will allow it to continue on to the next item after skipping those nested elements? Also, what if I created a class that only took the item id and item name, then a second class that stored a list of that class. Should that work fine? Commented Jan 22, 2018 at 17:08
  • You didnt show any code, so I have no idea how you are doing it, but the result would be an object with a collection of those things. Commented Jan 22, 2018 at 17:10
  • Okay then I misunderstood what you meant by "omit." Alright so I still need the full structure. I guess making it into a list of those objects and then copy only needed data into another list, then disposing of the original would likely be the best route I take it. Commented Jan 22, 2018 at 17:12
  • No, you can define props for the just data you want. If you dont want the actions collection, dont include it. The result is already going to be a )object containing a) collection so you dont need to worry about that part Commented Jan 22, 2018 at 17:13

2 Answers 2

2

You could ignore the properties which you don't want to deserialize or you can remove the property from target class.

public class Inventory
{
    public string fourthop { get; set; }
}

public class World
{
    public string secondop { get; set; }
}

public class Actions
{
    public Inventory inventory { get; set; }
    public World world { get; set; }
}

public class RootObject
{
    public int id { get; set; }
    public string name { get; set; }
    public bool exchangeable { get; set; }
    public bool members { get; set; }
    public int placeholder_id { get; set; }
    [JsonIgnore]//Ignore deserialization
    public Actions actions { get; set; }
}

var result = JsonConvert.DeserializeObject<List<RootObject>>(json);
Sign up to request clarification or add additional context in comments.

4 Comments

Ahh I never learned about that. Thanks a million you literally saved me a lot of extra time figuring out how I should go about this. So my original code is basically already correct I just need to make a couple small changes and I'll have it set. Thanks a ton
You don't need to change anything in your code, unless you want to change the design. [JsonIgnore] is used to tell the serializer to not serialize those specific properties EVER, mostly because it cannot or shouldn't be serialized. If you simply don't provide values to some properties, they will not be included in the serialization anyway, and no code change is required.
That's what I meant by changing my code. Just adding those attributes to the properties. Haha
@Matthew Arnold, you're welcome. Mark the answer as accepted if it worked.
0

"Is there a way to simply use a class resembling only the root object without those 3 areas and still have it all deserialize properly?"

Yes, you can just deserialize the values you want, straight to a list: here's an example:

void Main()
{
    var json = File.ReadAllText(@"c:\temp\json.txt"); // your json
    var output = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Target>>(json);
}

public class Target
{
    [Newtonsoft.Json.JsonProperty("id")]
    public int Id { get; set; }

    [Newtonsoft.Json.JsonProperty("name")]
    public string Name { get; set; }

    [Newtonsoft.Json.JsonProperty("exchangeable")]
    public bool Exchangeable { get; set; }

    [Newtonsoft.Json.JsonProperty("members")]
    public bool Members { get; set; }

    [Newtonsoft.Json.JsonProperty("placeholder_id")]
    public int PlaceholderId { get; set; }

    [Newtonsoft.Json.JsonProperty("noted_id")]
    public int? NotedId { get; set; }
}

And the output:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.