我正在尝试运行一个简单的杰克逊解析器,但是当我试图从 Json 文件中获取数据时,它会告诉“\user.json(没有这样的文件或目录)”。
杰克逊解析器:
public class JacksonParser {
static ObjectMapper mapper = new ObjectMapper();
public static User mapping() throws JsonParseException,
JsonMappingException, IOException {
User user = mapper.readValue(new File("user.json"), User.class);
if (user == null)
return user;
return user;
}
}
User.java(pojo 类):
public class User {
public enum Gender {
MALE, FEMALE
};
public static class Name {
private String _first, _last;
public String getFirst() {
return _first;
}
public String getLast() {
return _last;
}
public void setFirst(String s) {
_first = s;
}
public void setLast(String s) {
_last = s;
}
}
private Gender _gender;
private Name _name;
private boolean _isVerified;
public Name getName() {
return _name;
}
public boolean isVerified() {
return _isVerified;
}
public Gender getGender() {
return _gender;
}
public void setName(Name n) {
_name = n;
}
public void setVerified(boolean b) {
_isVerified = b;
}
public void setGender(Gender g) {
_gender = g;
}
}
主要活动:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textview = (TextView) findViewById(R.id.textView);
User user = null;
try {
user = JacksonParser.mapping();
} catch (Exception e) {
e.getStackTrace();
}
textview.setText("" + user.getName() + " " + user.getGender() + " " + user.isVerified());
}
}
用户.json:
{
"name" : { "first" : "Joe", "last" : "Sixpack" },
"gender" : "MALE",
"verified" : false,
}
更新:我更正了上一个错误,但坚持下一个错误。它告诉“\user.json(没有这样的文件或目录)”我已经更新了这个问题。