读取json文件

需求:用户在页面上选择本地json文件后,进行解析和展示。
解决方案;1、利用js直接对json文件进行解析。2、ajax把文件传送到服务器,利用java解析json文件,然后返回数据。

hospital.json文件示例

{
    "hospital":[
        {
            "lon":120,
            "lat":30,
            "name":"西安医院"
        },
        {
            "lon":130,
            "lat":40,
            "name":"北京医院"
        }
    ]
}

html代码



1、js解析json文件



2、ajax上传文件,java解析

2.1 ajax代码



2.2 springMVC框架搭建

读取json文件_第1张图片
image.png

pom.xml




  4.0.0
  war

  filetemp
  FileTemp
  filetemp
  1.0-SNAPSHOT

  
    
      
        org.apache.tomcat.maven
        tomcat7-maven-plugin
        2.2
      
    
  

  
    
    
      org.springframework
      spring-context
      5.0.0.RELEASE
    
    
      org.springframework
      spring-core
      5.0.0.RELEASE
    
    
      org.springframework
      spring-beans
      5.0.0.RELEASE
    
    
      org.springframework
      spring-web
      5.0.0.RELEASE
    
    
      org.springframework
      spring-webmvc
      5.0.0.RELEASE
    
    
      org.springframework
      spring-aop
      5.0.0.RELEASE
    
    
      org.springframework
      spring-tx
      5.0.0.RELEASE
    
    
      org.springframework
      spring-jdbc
      5.0.0.RELEASE
    

    
    
      commons-fileupload
      commons-fileupload
      1.3.2
    

    
      org.json
      json
      20180130
    
  


spring-mvc.xml代码




    
    

    
    

    
    
        
        
        
        
        
        
    

web.xml代码




    
    
        encodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
    
    
        encodingFilter
        /*
    

    
        contextConfigLocation
        classpath:config/spring-mvc.xml
    

    
    
        SpringMVC
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:config/spring-mvc.xml
        
        1
    
    
        SpringMVC
        /
    

    
    
        org.springframework.web.context.ContextLoaderListener
    
    
    
        org.springframework.web.util.IntrospectorCleanupListener
    


    
        /WEB-INF/jsp/index.jsp
    

     

2.3 java解析文件代码

FileUploadController.java代码

package controller;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;


@Controller
@RequestMapping("/file")
public class FileUploadController{

    @ResponseBody
    @RequestMapping(value="/upload", method= RequestMethod.POST,produces="text/html;charset=UTF-8")
    public String fileUpload(@RequestParam("myfile") MultipartFile file){
        String fileName = file.getOriginalFilename().substring(0,file.getOriginalFilename().length()-5);
        String str = "";
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(file.getInputStream(),"UTF-8");
            BufferedReader br = new BufferedReader(inputStreamReader);
            String s= "";
            while ((s = br.readLine()) != null) {
                str +=s;
            }
            br.close();
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject jo = new JSONObject(str);
        jo.put("fileName",fileName);
        System.out.println(jo.toString());
        return jo.toString();
    }
}

你可能感兴趣的:(读取json文件)