如何从包含json格式数据的任何浏览器下载text / json文件

时间:2022-03-27 19:35:48

I want to download a text/json file using string that contains the data in json format.

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

I am adding a ArrayList containing the a object of Service Class to the model from controller. Below is the code.

我正在从控制器向模型添加一个包含Service Class对象的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);

 }

Now I want to download each jsonData file.

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

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

On Clicking any button a text/json file containing the relevant data should download.

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

And the data in file should be in proper JSON format like:

文件中的数据应采用适当的JSON格式,如:

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

I am unable to retrieve the data in JSON format. please help me to sort out this.

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

1 个解决方案

#1


0  

Try some thing like this:

尝试这样的事情:

$json = json_encode( array( 'test' => 'test' ));

header('Content-disposition: attachment; filename=jsonFile.json');
header('Content-type: application/json');

echo( $json);

#1


0  

Try some thing like this:

尝试这样的事情:

$json = json_encode( array( 'test' => 'test' ));

header('Content-disposition: attachment; filename=jsonFile.json');
header('Content-type: application/json');

echo( $json);