【问题标题】:Error with parsing json解析json时出错
【发布时间】:2018-11-06 17:45:09
【问题描述】:

我写代码来读取json文件,但是有错误:

private static void parseJSON(JsonParser jsonParser, Response resp, Result res) throws JsonParseException, IOException
    {
        try{        
        ObjectMapper mapper = new ObjectMapper();
        JsonFactory f = new JsonFactory();

        List<Result> ress = null;
        JsonParser jp = new JsonFactory().createParser(new File("C:\\Users\\Ismr\\work2\\response.json"));
        TypeReference<List<Result>> tRes = new TypeReference<List<Result>>() {};
        ress=mapper.readValue(jp, tRes);

        for (Result result : ress) {
            System.out.println(result.toString());
        }

    } catch (JsonGenerationException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

所有库安装。我做错了什么?

【问题讨论】:

  • 错误说明?
  • 尝试使用 readValues() 而不是 readValue()。
  • @DerickDaniel 错误-配置图片路径,但我安装了 jackson-mapper-asl-1.9.13.jar,readValues() 不起作用
  • 清理并构建您的项目
  • “配置构建路径”是 Eclipse 的建议。它不是错误信息。请告诉我们完整的错误信息!我猜,你得到一个“未定义的符号”,这仅仅意味着所需的库不是你的构建路径的一部分,或者你使用了错误的元素。

标签: java json jackson


【解决方案1】:

使用您可以编写的 Json 简单库。 你没有给出如何成为你的 json。 甚至本准则也可能对您有所帮助。

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.io.FileWriter;
import java.io.IOException;

    public class JsonHelper {
        public static void main() {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("Key","value");
            jsonObject.put("Key1","value1");

            JSONArray sensorJsonArray = new JSONArray();
                JSONObject simple = new JSONObject();
                simple.put("JsonArrayKey","JsonArrayValue");
                simple.put("JsonArrayKey1","JsonArrayValue1");
                simple.put("JsonArrayKey2","JsonArrayValue2");
                sensorJsonArray.add(simple);

            try(FileWriter file = new FileWriter("C:\\Users\\Ismr\\work2\\response.json")) {
                jsonObject.put("Array", sensorJsonArray);
                file.write(jsonObject.toJSONString());
                file.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-07
    • 2017-05-04
    • 1970-01-01
    相关资源
    最近更新 更多