【问题标题】:How to download a text/json file from any browser that contains data in json format如何从包含 json 格式数据的任何浏览器下载 text/json 文件
【发布时间】:2017-04-08 06:00:08
【问题描述】:

我想使用包含 json 格式数据的字符串下载一个 text/json 文件。

我正在从控制器向模型中添加一个包含服务类对象的 ArrayList。下面是代码。

 @RequestMapping("/Application.html")

 public ModelAndView getdetails() throws Exception {

      ArrayList<Service> servicesList = new ArrayList<Service>();
      ServiceBean service=new ServiceBean();    // This is bean class

      String jsonData = "{\"menu\": {  \"id\": \"file\",  \"value\": \"File\",  \"popup\": {    \"menuitem\": [      {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},      {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},    ]  }}}";
      service.setJsonData(jsonData);
      servicesList.add(service);


      ServiceBean service=new ServiceBean();    // This is bean class
      String jsonData = "{    version-info: {        minVersion: 2.0.1,        currentVersion: 2.0.1,        configuration: {            id: {},            language: {},            url: {},            version: 2.0.1        },        clientApp: ABC    }}";
      service.setJsonData(jsonData);
      servicesList.add(service);

      ModelAndView mav = new ModelAndView("showVersion");
      mav.addObject("servicesList",servicesList);

 }

现在我要下载每个 jsonData 文件。

 <button>Download First File</button>
 <button>Download Second File</button>

单击任何按钮时,应下载包含相关数据的文本/json 文件。

并且文件中的数据应该是正确的 JSON 格式,例如:

{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}

我无法检索 JSON 格式的数据。请帮我解决这个问题。

【问题讨论】:

    标签: javascript java jquery html json


    【解决方案1】:

    尝试这样的事情:

    $json = json_encode( array( 'test' => 'test' ));
    
    header('Content-disposition: attachment; filename=jsonFile.json');
    header('Content-type: application/json');
    
    echo( $json);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-12
      • 1970-01-01
      • 2020-09-01
      • 1970-01-01
      • 2015-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多