当前位置: 代码迷 >> Java相关 >> 初学者上路,请赐教!java读写本地json文件
  详细解决方案

初学者上路,请赐教!java读写本地json文件

热度:9838   发布时间:2013-02-25 21:47:08.0
菜鸟上路,请各位高手赐教!java读写本地json文件
11. programmers.json文件中包含如下内容:
{ "programmers": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "brett@newInstance.com" }, { "firstName": "Jason", "lastName":"Hunter", "email": "jason@servlets.com" }, { "firstName": "Elliotte", "lastName":"Harold", "email": "elharo@macfaq.com" } ],"authors": [ { "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" }, { "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" }, { "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" } ],"musicians": [ { "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" }, { "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" } ]}
从文件获取programmers、authors 和 musicians的信息,做以下处理:
1. 在控制台输出Jason,Isaac,Sergei的相关信息(firstName,lastName等)
2. 将分析得到的programmers、authors 和 musicians的信息,以json格式输出到people.json文件中
3. 将programmers.json的内容转换为XML格式,根元素为data.
求该题的完整代码!新手刚入门,还请各位高手多多指教,不胜感激!

------解决方案--------------------------------------------------------
就是用file读取文件内容(应该是按行读取)

再用JSONObject解析就行了。
------解决方案--------------------------------------------------------
Java code
    private ObjectMapper mapper;    private List<AppConfig> appConfig;    public AppConfigController()    {        String propsFile = AppConfigController.class.getResource("/").toString().replace("file:", "") + "appConfig.cfg";        this.mapper = new ObjectMapper();        this.mapper.setAnnotationIntrospector(new NopAnnotationIntrospector());        try        {            propsFile = java.net.URLDecoder.decode(propsFile,"utf-8");                        logger.info("获取到配置文件路径{}", new File(propsFile).getAbsolutePath());            this.appConfig = mapper.readValue(new File(propsFile), new TypeReference<List<AppConfig>>()            {            });        } catch (Exception e)        {            e.printStackTrace();            logger.warn(e.getMessage() + "配置文件路径:{}", propsFile);        }    }
------解决方案--------------------------------------------------------
解决了?那就结贴吧
探讨

哦 非常感谢
  相关解决方案