开发者

How to apply javascript formatting to .json files?

开发者 https://www.devze.com 2023-04-06 23:40 出处:网络
The Ctrl + Shift + F hotkey in Eclipse can format a file. It doesn\'t work for .json files. How to mak开发者_Go百科e it work?You\'ll want to get the JSON Editor plugin if you don\'t already have it. Y

The Ctrl + Shift + F hotkey in Eclipse can format a file. It doesn't work for .json files. How to mak开发者_Go百科e it work?


You'll want to get the JSON Editor plugin if you don't already have it. You can find it here

The JSON Editor is a simple plugin for the Eclipse IDE that provides: - Color text highlighting - An Outline Tree view - JSON validation - Text formatting - Text folding for the JSON data format.

If the hot keys still don't work. Take a look under the menu as shown in the picture from their site here

Also, I see that there has been at least one issue with what looks to be the current versions formatting feature in the past. From their discussion site:

rlespinola 
2010-07-15 00:18:05 UTC
Using version 0.9.4, I do not see the option to "Format Text". Also, when I open a .json file, the outline view says "An outline is not available".

jdschulteis
2010-12-27 16:59:24 UTC
Using 0.9.4 on Helios, I also had "An outline is not available". I went to Window->Preferences->General->Editors->File Associations, selected '*.json' in the 'File types:' list, selected 'Json Editor' in the 'Associated editors:' list, and clicked 'Default'. I now get an outline, though it can be somewhat slow (6K line file).

Edit:

There are several ways to add a plugin. Eclipse recommends using the update manager. Try help > software updates > find and install

You can look here for some alternative methods.


There are two options I figured out using Eclipse Luna (4.4.0).

Use a JSON Editor Plugin and define shortcuts

  • Download and install the JSON Editor Plugin from sourceforge manually or use the Eclipse marketplace (Help -> Eclipse marketplace) to find and install the plugin
  • Go to Window -> Preferences -> General -> Keys and filter for "format text".
  • Choose "Format Text" and set the "When:" value to "Editing Text" (sadly there is no explicit condition for JSON editing, but the format event for the JSON Editor is different to the other editors, so "Editing Text" will work aswell)
  • Set the "Binding:" to Ctrl + Shift + F

Use the Javascript Development Plugin with an ugly and anoying workaround

  • Get the plugin using Help -> Install New Software -> Work with: "http://download.eclipse.org/releases/luna" -> Programming Languages -> JavaScript Development Tools
  • Associate *.json files with the JavaScript Editor (Window -> Preferences -> General -> Editors -> File Associations)
  • You can now create files with "json" extension and edit them in Eclipse using the JavaScript Editor, but formatting with Ctrl + Shift + F will not work directly with the following unformatted example:

{"addressbook": {"name": "John Doe",

    "address": {

        "street": "5 Main Street", "city": "San Diego, CA", "zip": 91912
    },
    "phoneNumbers": [
        "619 555-3452",
        "664 555-4667"
    ]
 }
}

  • The hack is to create a valid JavaScript variable from the object description like this:

var obj =    {"addressbook": {"name": "John Doe",

    "address": {

        "street": "5 Main Street", "city": "San Diego, CA", "zip": 91912
    },
    "phoneNumbers": [
        "619 555-3452",
        "664 555-4667"
    ]
 }
}

  • Using Ctrl + Shift + F will now work
  • In the end you will have to remove the "var obj =" hack to make the JSON file valid again


You could use the JavaScript editor that ships with Eclipse.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号