11

I have a project I have been working on using Xamarin's MonoDevelop.

I have been using Newtonsoft's Json nuget package.

I just downloaded Visual Studio 2017 for Mac.

I try to build my project in VS2017Mac and get the following error:

error CS0433: The type 'JsonConvert' exists in both 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' and 'System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

I thought I should be able to fix that by just adding Newtonsoft.Json. to the front of JsonConvert, but that didn't work.

I don't want to remove Newtonsoft's implementation if possible because I think their library still has more functionality. Is there another way to resolve this? Why didn't adding the full assembly reference work?

weir
  • 4,521
  • 2
  • 29
  • 42
user856232
  • 1,073
  • 2
  • 14
  • 40

4 Answers4

5
  1. In the Properties window for the project's Newtonsoft.Json reference, change the value of Aliases from global to global, foo.

  2. Insert extern alias foo; as the first line of any class that consumes Newtonsoft.Json.

  3. Qualify members with foo.. Example: foo.Newtonsoft.Json.JsonConvert.SerializeObject(someObject)}.

weir
  • 4,521
  • 2
  • 29
  • 42
5

I had the following error message but with another library(Ranet) in C#:

Error CS0433 The type 'JsonConvert' exists in both 'Microsoft.AnalysisServices.Tabular.Json, Version=14.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' and 'Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'

I solved it using aliases, but I would like to provide a bit more detail as I struggled to implement using the instructions from these answers and other answers. This is how I did it:

  1. In the solution explorer, right click on the "Newtonsoft.json", add ", Newton" to the alias field, it should look something like this:

enter image description here

  1. Add the following code to the first line of you file(before all using statements):
extern alias Newton;
  1. Add the following reference to the end of your using statements:
using NewtonReference = Newton::Newtonsoft.Json;
  1. Now you can call Newtonsoft method using the following code:
NewtonReference.JsonConvert.DeserializeObject<string>("");
  1. A final example would look something like this:
extern alias Newton;

using System;
using NewtonReference = Newton::Newtonsoft.Json;

public class Test {     
    public static List<string> TestMethod() {
        NewtonReference.JsonConvert.DeserializeObject<string>("");  
    }  
}

Hopefully this will be helpful to someone else :)

David Rogers
  • 2,601
  • 4
  • 39
  • 84
0

If anyone is still looking for a solution, I went to the project references and it showed two different versions of Newtonsoft (versions 12.0.0 and 6.0.0 for me). I clicked on the older version and it showed me the file path to where it was stored. I simply went there and deleted it. Once that was done I had the same problem with version 9.0.0 and did the same thing. When I opened it up again the problem was fixed.

-1

install previous ver of newtonsoft.json package.

mustafatorun
  • 313
  • 2
  • 7