I have found many examples of code for JSON.NET, but I am unable to make any of them run in Visual Studio (C#). Quite probably this is because I am missing something obvious in how to code.
Particularly frustrating is the example code in newtonsoft.com will not compile and run. For example http://www.newtonsoft.com/json/help/html/SerializeObject.htm contains code for 'Types' and 'Usage' but there are no using statements and I can't figure out how to put the code into a project in a way to make it work.
I am sure I am missing something basic I just can't figure it out. I have been Googling for an answer for three days. Can you help me?
Assuming that you have downloaded the Json.NET nuget package (or otherwise properly added Newtonsoft's package to your project and referenced it) the only using statement you need other than the standard ones visual studio adds for you is Newtonsoft.Json
Their code runs perfectly fine in a project of type console application with the following using's:
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
The only other thing you might want to do is add a Console.ReadLine(); at the end so that you can see the output.
As I attempted to reply to your questions I created a new C# Console Application Solution named CA_Json.
I right clicked on the project named ‘CA_Json’ and selected ‘Manage NuGet packages…’.
I then selected ‘Newtonsoft.JSON’ and clicked the ‘Install’ button.
Then this page appeared http://www.newtonsoft.com/json/install?version=8.0.2 and Newtonsoft.JSON appeared under References
So I believe I have added JSON successfully.
In Program.cs I added this:
• using Newtonsoft.Json;
• the code for Types
• and the code for Usage
I compiled it and then started debugging and it worked! I went back and checked my prior project and somehow the Newtonsoft.JSON Did NOT appear under References, even though I had added it earlier.
Thanks so much! Your questions forced me to carefully review my work and discover my error. :-)
Related
I'm trying to add some JSON parsing to the C# code in VS2015 but I can't find:
System.Web.Helpers
as the MS documentation suggests. I've looked at other people's questions of a similar nature (almost all on earlier versions) and the answers all say to add it in the Assemblies / Framework dialog.
The problem is it doesn't appear in that dialog, nor do many of the other things I see people talking about.
I would really appreciate any help on fixing this.
Here is the list I have access to:
Edit: Added the Extensions window as well
This question is a bit old but here's a simple solution - Microsoft seemed to just move this library to a Nuget package called "microsoft-web-helpers". Installing that package seems to be equivalent to having the assembly.
Old question but might still help someone.
You must install MVC 3 in order to have the System.Web.Helpers Extension on VS2015.
link to download: https://www.microsoft.com/en-us/download/details.aspx?id=1491
You cannot find it because of the simple fact that's not part of the Framework assemblies (what you are looking at).
It's part of the Extensions assemblies, and, as posted here, you will find it under "Extensions" in the left-hand side of the dialog.
I was wanting to work with JSON as well in an ASP.Net project using VS2015.
I ended up here trying to find answers. Right about the time I finished reading the other answers on this post I noticed my project already had a reference to Newtonsoft.Json. Never heard of it before so I looked it up; I'm glad I did.
It was literally a one liner to Serialize a List<String> to JSON.
You can pass in an Object so you should be able to serialize most any Object within reason.
Ultimately I didn't need to install MVC3 or reference System.Web.Helpers
Hope this helps!
NewtonSoft Serializing and Deserializing JSON
I stumbled upon the same issue and fixed it by adding NuGet Package ->
Microsoft.AspNet.WebPages
I've ran into this issue a couple times and I'm wondering if anyone has a better solution than trial and error or searching stack overflow.
Lets say we are using some .net class Foo
Foo resides in the Bar.Baz namespace
The following statement
using Bar.Baz;
is not sufficient to compile the program, we are missing an assembly reference. So add a reference to System.Bar.Baz It still doesn't work so after searching the internet I find that I actually have to add a reference to Some.Other.dll and now it compiles.
My question is how do I know what namespace maps to what reference when the usual one doesn't work?
Most recent problem was
The type or namespace name 'DbContext' could not be found Instead of adding a reference to System.Data.Entity I had to install through Nuget.
If it is a .NET framework function, you can just search it on MSDN, and it will tell you in which assembly the class/function exists.
You can also use ReSharper which is a very nice plugin to Visual Studio, and it can help you add assemblies automatically.
If you're using Visual Studio 2013 or higher, one easy way to discover which namespace a class belongs to is using the Peek definition feature. You can easily find it in the right-click context menu.
In the screen below, I used it with KeyValuePair:
Also, take a look at the documentation.
I'm in C# land today. I'm trying to write a function which accepts a user agent string and returns an object that gives me at least the browser name and version. So I tried this answer, but apparently I don't have access to HttpBrowserCapabilities. It tells me the type or namespace name could not be found (yes, even if I add using System.Web, it still doesn't show up, or when I type using System.Web. it doesn't pop up, so it's obviously not there).
I'm using .net 3.5, but the documentation for that class shows it existed even in 3.5, so I'm not sure what's going on. I have access to the browscap files - ini or xml. Am I going to have to write this from scratch?
Edit: I've fixed the reference problem. But Chrome is being reported as AppleMAC-Safari 5.0. I'm wondering if I'm going to need a completely different approach. PHP figures it out with the same ini file fine.
Adding a using block does not automatically import the DLL. All a using does is allow you to not write:
System.Web.HttpClient //Or whatever
All over the place, and use HttpClient instead.
You need to add a reference to System.Web to the project before any of its classes will be available to you.
Did you have a using System.Web; statement in your source file?
Here's a tip: if you're using Visual Studio, and you have a reference to the System.Web.dll in your project, if you type the name of a type and press Ctrl-. it will give you a popup menu to add the namespace reference to your source file.
Do you see it in the ObjectBrowser (assuming you are using Visual Studio)? I found the namespace this morning (granted I'm on 4.5 - but documentation shows it has been around since 3.5 and earlier)
I've seen the above question asked many times on many sites, but I haven't seen an answer that fixed the problem.
The scenario is this...
I am on .NET Framework 4.0, building a C# web application in VisualStudio 2012 Express with the Razor view engine.
I'm trying to use Microsoft.VisualBasic.FileIO.TextFieldParser in my code. From what I've read it is appropriate to do so by adding a project reference to Microsoft.VisualBasic, which I've done, and coding #using Microsoft.VisualBasic; in my view. However when I code...
Microsoft.VisualBasic.FileIO.TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(new StringReader(xxxxxxxx));
...in the view and rebuild the solution it returns errors: The type or namespace name 'FileIO' does not exist in the namespace 'Microsoft.VisualBasic' (are you missing an assembly reference?)
I know the reference is in the correct project because when I add and remove it I see it come and go from the references folder. I know the code I'm trying to reference exists because I can see it in Object Browser. I've even tried adding the reference using Object Browser instead of through the menus and although it gets added, the results are the same.
The solution it's in is a bit complicated so I tried just creating a test stand alone C# project and I get the same results. I also tried creating a test stand alone Visual Basic project, and sure enough, it works there as advertised. I'm by no means an expert on Visual Studio so I'm guessing that it's just something I'm missing in the configuration, but I'm at a loss to figure it out.
I've spent days on this, so any help anyone could offer would be greatly appreciated.
Right-click on your project and select Add Reference...
In the Reference Manager, expand Assemblies and select Framework. Then check the box for Microsoft.VisualBasic and click OK.
Credit goes to:
https://stackoverflow.com/a/17146200/2530360
I just had the same problem and your question title was more verbose so I figured it should have the answer inside.
In C#, I made a ClassLibrary that has one Namespace and one Class.
I saved it and build it.
in other Projects, when i use it, I add it to my references by browsing to the .dll location.
But The Problem is that its name is not showing up in the Intellisense.
i.e when I: using ... my dll doesn't show ..
I'm Importing the library to a ConsoleApp.
both of the App and the library target Framework is .NET Framework 4.0
and I made their Assembly Version 4.0.0.0 so they're the exact same.
is there a setting or something that I'm missing ?
how can i make it pop up ?
I'm using VS2010 Professional
Thank you for your help
Maybe this be usefull, I was having a similar issue, I have a Web project, add the reference to a Class Library by selecting the project, but if I made a change on the class library, I can´t see that change on the intellisense of the Web Project, after try many things, I see that in the recently added reference, the value of the option "Local Copy" was set "True", then I change it to false and everething works!
I had a similar issue but in my case it was a property on the class. If you go to the file properties and look for a Build Action. Somehow mine was set to Content it had to be set to Compile.
I am using Visual Studio 2013. I hope this helps someone else.
Is the namespace for your assembly different than the namespace for your currently open project? I've had times when the current project and an assembly share the same namespace path the intellisense can mess up.
In general, Visual Studio is pretty good about intellisense generation, especially for C#. But sometimes there are some interesting conditions regarding ambiguities, and especially mixing project types where it just doesn't quite work.
Placing your content in the same namespace makes me wonder if you've actually fixed the problem (it may just be autocompleting the namespace in the currently loaded project rather than the assembly), but if it allows you to continue working, then go with it!
Right click on project on which you add reference of your dll/project select menu project dependancies and select/MarkCheckBox for reference project/dll. then it will work fine.
If the class library project had its name changed after creation, then intellisense may fail finding it due to directory issues, I believe.
I created my class with the generic "ClassLibrary1" or whatever, and then later changed the default namespace, class name, and project name inside of VS2017. I closed VS2017 and changed the directory name to match my default namespace, and then re-associated the project file in VS2017, and then re-added the reference in my main project file.
All seems to be fixed now.