Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

Bug: 'JObject' does not contain a definition for 'ContainsKey' #80

Open
2 tasks done
ghostravenstorm opened this issue Feb 5, 2021 · 4 comments
Open
2 tasks done
Labels
bug Something isn't working

Comments

@ghostravenstorm
Copy link

ghostravenstorm commented Feb 5, 2021

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

  • Shutdown Unity, deleted the /Library folder, opened project again in Unity, and problem still remains.
  • Checked to be using latest version of the package.
@ghostravenstorm ghostravenstorm added the bug Something isn't working label Feb 5, 2021
@applejag
Copy link
Owner

applejag commented Feb 6, 2021

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!

@ghostravenstorm
Copy link
Author

image

@nicovv44
Copy link

nicovv44 commented Jul 19, 2021

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;

@applejag
Copy link
Owner

applejag commented Jul 19, 2021

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants