Java Jackson 面向 Eclipse 初学者

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21633677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:47:08  来源:igfitidea点击:

Hymanson for beginners in Eclipse

javajsoneclipseHymanson

提问by ViRALiC

let me just say that I'm still learning Java and the subtleties of Eclipse, and I come to you because I am unsure how to properly phrase my question to Google or to StackOverflow's search engine. Apologies if its infinitely trivial.

我只想说我还在学习 Java 和 Eclipse 的微妙之处,我来找你是因为我不确定如何正确地向 Google 或 StackOverflow 的搜索引擎表达我的问题。道歉,如果它无限微不足道。

I am trying to understand the process of converting JSON-format strings into objects in Java. I found following example online:

我试图了解在 Java 中将 JSON 格式的字符串转换为对象的过程。我在网上找到了以下示例:

import java.io.IOException;

import org.apache.log4j.Logger;
import org.codehaus.Hymanson.JsonParseException;
import org.codehaus.Hymanson.map.JsonMappingException;
import org.codehaus.Hymanson.map.ObjectMapper;public class JsonToJavaConverter {

private static Logger logger = Logger.getLogger(JsonToJavaConverter.class);

public static void main(String args[]) throws JsonParseException,
        JsonMappingException, IOException {

    JsonToJavaConverter converter = new JsonToJavaConverter();

    String json = "{\n" + "    \"name\": \"Garima\",\n"
            + "    \"surname\": \"Joshi\",\n"
            + "    \"phone\": 9832734651}";

    // converting JSON String to Java object
    converter.fromJson(json);
}

public Object fromJson(String json) throws JsonParseException,
        JsonMappingException, IOException {
    User garima = new ObjectMapper().readValue(json, User.class);
    logger.info("Java Object created from JSON String ");
    logger.info("JSON String : " + json);
    logger.info("Java Object : " + garima);

    return garima;
}

public static class User {
    private String name;
    private String surname;
    private long phone;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public long getPhone() {
        return phone;
    }

    public void setPhone(long phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "User [name=" + name + ", surname=" + surname + ", phone="
                + phone + "]";
    }
}}

Now, here's the silly part (please don't string me up for asking):

现在,这是愚蠢的部分(请不要问我):

import org.apache.log4j.Logger;
import org.codehaus.Hymanson.JsonParseException;
import org.codehaus.Hymanson.map.JsonMappingException;
import org.codehaus.Hymanson.map.ObjectMapper;

Are all underlined in red with Eclipses only hint, being that "The import org.apache.log4j cannot be resolved".

都用红色下划线加上 Eclipses 的唯一提示,即“无法解析导入 org.apache.log4j”。

As a newcomer to both Eclipse and Java, this leaves me absolutely dumbstruck.

作为 Eclipse 和 Java 的新手,这让我完全目瞪口呆。

Could anyone please tell me what needs doing to resolve this basic issue?

谁能告诉我需要做什么来解决这个基本问题?

Deeply appreciated.

深表赞赏。

采纳答案by superEb

You need to add the libraries (jar files) to the project's build path in Eclipse.

您需要将库(jar 文件)添加到 Eclipse 中项目的构建路径。

You can find these libraries in Maven Central here:

您可以在此处的 Maven Central 中找到这些库:

Log4j

日志4j

Hymanson

Hyman逊

回答by Paul Hicks

You need to add the log4j jar to your classpath. Assuming that you're not using maven or gradle, you can download it from Apache's site. Then put it in some suitable shared location and add it to your project's classpath. I think it's about the 3rd or 4th item in the Project Properties dialog, iirc.

您需要将 log4j jar 添加到类路径中。假设您没有使用 maven 或 gradle,您可以从Apache 的站点下载它。然后将其放在某个合适的共享位置并将其添加到项目的类路径中。我认为这是关于项目属性对话框 iirc 中的第 3 项或第 4 项。

回答by M21B8

You need to add the relevant Jar files to your projects classpath

您需要将相关的 Jar 文件添加到您的项目类路径中

http://javahowto.blogspot.co.uk/2006/06/set-classpath-in-eclipse-and-netbeans.html

http://javahowto.blogspot.co.uk/2006/06/set-classpath-in-eclipse-and-netbeans.html