Skip to content

How to check if JSONNode contains a key quickly? #4

@ROBYER1

Description

@ROBYER1

Hey, been using this repo for a while in projects and it works a treat, I was looking at ways to check quickly if a JSON Node contains a key, and wasn't able to find a way to do that with your version - I tried the below script but there is no definition for node.Keys in your repo.

This works with the Bunny83 SimpleJSON latest version, but we ran into another issue with their version of SimpleJSON where checking if the JSON Node was null in an if statement always returned false even if the JSON was empty! Bunny83/SimpleJSON#45 - I have reported that issue to them there referencing your repo

So your version is great for checking if a JSON Node is null, but there is no way I could see to check quickly if a JSONNode contains a key with yours other than looping through the JSON Node by a section I defined and checking if values contain a string which feels slow - any tips?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace SimpleJSON
{
public class JSONNodeExtension : MonoBehaviour {

	public static bool ContainsKey(SimpleJSON.JSONNode node, string checkKey)
	{
		foreach (string key in node.Keys)
		{
			if (key == checkKey)
				return true;
		}
		return false;
	}

}

}

Thanks for the great and robust version of SimpleJSON

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions