2

Is it possible to get IntelliSense in Visual Studio 2010 working for JSON data? I know there is a workaround for third party scripts which has come in very handy.

In asp.net mvc 3, I serialize data in a controller and then pass it in the view model. I access it like this:

var JSONData = @( Html.Raw( Model.JSON ) );

Inside of this JSONData is a complex object graph with several levels of nesting. I may want to get a list of happy campers like this:

var HappyCampers = JSONData.Foo.Bar.HappyCampers;

Is there a way for IntelliSense to show that Foo is available to JSONData, that Bar is available to Foo, and that HappyCampers is available to Bar?

Travis J
  • 81,153
  • 41
  • 202
  • 273
  • 1
    I don't think the problem is intellisense, it just doesn't know the value of Model.JSON before runtime. if it was a direct javascript object then intellisense could try to make educated guess (at least Resharper does) but in your case there is no way for it to know at compile time. At runtime, you could always use Firebug or Chrome tools autocomplete in the browser's console anyhow. – kabaros Oct 24 '12 at 20:05
  • @kabaros - Yes, at runtime I can tell. And because c# constructs it, I can tell what will be there. However, there is the off chance of mistyping one of the objects such as HappyCamper on accident. I did not think so either, but figured if someone knew, it would be here. I looked through google and through SO but could not find any workarounds or situations which this was available. As you say, it could really be anything so how would IntelliSense know anyway? – Travis J Oct 24 '12 at 20:11

2 Answers2

2

Quoting Intellisense for JSON Schema in the JSON Editor:

When you open your JSON file in Visual Studio in editor, you can specify the schema within a JSON file using the “$schema” property inside your JSON data file. When you add a new JSON file to your project, the schema dropdown box will show <No Schema Selected> but you can select the schema you used before.

You can also specify the schema for a JSON file using the schema textbox instead of using the $schema property in the file and it can be faster. However, if you need to pass this same JSON file to another user, or use it in a different project, the schema specified in the schema textbox will not stick. You have to use the $schema property in order for the JSON editor to pick it up outside of your current project.

Once you point your JSON data file to corresponding schema you will be able to see in the Output window if the JSON editor failed to load the associated schema.

enter image description here

Rob
  • 26,989
  • 16
  • 82
  • 98
Dmitry Pavlov
  • 30,789
  • 8
  • 97
  • 121
0

JSON is not a strong-typed object so I don't believe there is any add-in that can help you with the IntelliSense.

What you can do is to deserialize the JSON in an object what will make it strong-typed and then you can use the IntelliSense.

Otherwise to assist with javascript there are a few plugins:

Visual Studio Javascript extensions feature comparison

JSEnhancements is awesome!

Also see this extension: http://code.google.com/p/js-addin/

which parses your script into an object tree that can be used for navigation.

Community
  • 1
  • 1
felipeclopes
  • 4,010
  • 2
  • 25
  • 35