Newtonsoft.Json-for-Unity
Newtonsoft.Json-for-Unity copied to clipboard
Bug: 'JObject' does not contain a definition for 'ContainsKey'
Expected behavior
Calling ContainsKey
on a JObject
should return true if the JObject
has the key.
https://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_Linq_JObject_ContainsKey.htm
Actual behavior
Unity complier doesn't recognize JObject.ContainsKey(string)
Steps to reproduce
- New project
- Import
jillejr.newtonsoft.json-for-unity
via UPM - Add following script to scene:
public class JsonTest : MonoBehaviour {
void Start () {
string jsonstr = "{\"name\":\"foo\"}";
JObject jobject = JObject.Parse(jsonstr);
bool containsKey = jobject.ContainsKey("name");
}
}
- Observe the error in the console
Details
Windows Unity Editor
Newtonsoft.Json-for-Unity package version 2.0.1
Unity 2019.4.18f1
Checklist
- [x] Shutdown Unity, deleted the /Library folder, opened project again in Unity, and problem still remains.
- [x] Checked to be using latest version of the package.
Hi @ghostravenstorm, thanks for reporting this!
I won't have time to check this out just yet but in the meantime could you also include the error from the console? Thanks!
Try adding
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
to your .csproj file.
And make sure to have
using Newtonsoft.Json.Linq;
Old response from me, excuse me for the delay, but this is probably because of usage of an older Newtonsoft.Json version. JObject.ContainsKey was added in v10 or v11 (don't recall 100%), so updating the package should be enough.
You say that you have
Newtonsoft.Json-for-Unity package version 2.0.1
But this repository in perticular has never had a 2.0.1 version. I'm guessing you meant the v12.0.1? But that version did have JQuery.ContainsKey:
https://github.com/jilleJr/Newtonsoft.Json-for-Unity/blob/d75074f30c1c65d9101c11aec2ac4273d07b2fa4/Src/Newtonsoft.Json/Linq/JObject.cs#L580-L590
Try upgrading if you can either way, as JamesNK and I have some bug fixes for the solution here and there since then.