DragonWar% 2018-05-27 20:52 采纳率: 0%
浏览 62

JSON文件的用例

I've used JSON a number of times within AJAX requests to perform asynchronous writes/reads of a database. I've been trying to better understand JSON and its uses within different programming environments and one of the questions I've been curious about is: what are the common use cases for JSON as external file (rather than just as an object that is passed within AJAX requests)?

More specifically, what are some use cases in which a .json file would be better suited than simply using temporary JSON objects to pass between AJAX requests? Any insight on this would be much appreciated.

  • 写回答

1条回答 默认 最新

  • 七度&光 2018-05-27 21:12
    关注

    I am not that familiar with AJAX etc., but JSON is so popular that many programming languages support it - not just Java and related languages.

    In itself JSON simply holds information - it's merely a format for storing data.

    It can often be used to transfer data between languages. Personally, I am also using JSON to store my objects to persistent data storages and then later on rebuild the objects alongside the .class schematics. For example, Google created GSON to easily turn objects into JSON and back. Very handy!

    You should also think about: How do you transfer an object from one machine to another?

    To sum it up: It's simple, it doesn't create massive overhead, it's even easy to read. And most important of all: So many tools offer JSON support.

    Edit:

    To show the simplicity of re-building from JSON, here's an example from my game:

    public static Player fromJson(String json) {
        if(json != null && !json.isEmpty()) {
            return gson.fromJson(json, Player.class);
        }
        return new Player(); //no save game present. Use default constructor
    }
    
    评论

报告相同问题?