如何在java中获取类的字段名
大家好抱歉我的语言不好 这是我的代码:如何在java中获取类的字段名,java,json,dynamic,reflection,field,Java,Json,Dynamic,Reflection,Field,大家好抱歉我的语言不好 这是我的代码: MyCustomClass temp = new MyCustomClass(); for (int i = 0; i < jsonarray.length(); i++) { JSONObject obj = jsonarray.getJSONObject(i); temp.ID = obj.getInt("ID"); temp.PicName = obj.getString("PicName"); temp.Pic
MyCustomClass temp = new MyCustomClass();
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject obj = jsonarray.getJSONObject(i);
temp.ID = obj.getInt("ID");
temp.PicName = obj.getString("PicName");
temp.PicURL = obj.getString("PicURL");
Items.add(temp);
}
MyCustomClass temp=新的MyCustomClass();
for(int i=0;i
我想采取这种动态
像这样的
MyCustomClass temp = new MyCustomClass();
Field[] myFields= MyCustomClass.class.getFields();
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject obj = jsonarray.getJSONObject(i);
for(int j=0;j<myFields.lenghth();j++)
{
myFields[j]=obj.getString(myFields[j].toString());
Items.add(temp);
}
}
MyCustomClass temp=新的MyCustomClass();
字段[]myFields=MyCustomClass.class.getFields();
for(int i=0;i 对于(int j=0;j您可以使用此结构获得所有类字段:
Class class = ...//obtain class object
Field[] methods = class.getFields();
在你的课堂上:
MyCustomClass temp = new MyCustomClass();
Field[] methods = temp.getFields();
使用jackson库,您可以直接使用json注释设置POJO,并且可以将json字符串直接转换为java对象
解析的一般方法可以是这样的:
public static <T> T deserialize(T t, Class<T> clazz, String json) throws JsonParseException, JsonMappingException, IOException{
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(json, clazz);
}
MyCustomClass myCustomClass= new MyCustomClass();
myCustomClass= JsonUtil.deserialize(myCustomClass, MyCustomClass.class, json);
@JsonIgnoreProperties // ignores properties from json String which are not in your Pojo
public class MyCustomClass {
@JsonProperty("anotherNameIfFieldNameIsNotEqual")
private String picName;
private String picURL;
public String getPicName() {
return picName;
}
public void setPicName(String picName) {
this.picName = picName;
}
public String getPicURL() {
return picURL;
}
public void setPicURL(String picURL) {
this.picURL= picURL;
}
}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
您的Pojo可以如下所示:
public static <T> T deserialize(T t, Class<T> clazz, String json) throws JsonParseException, JsonMappingException, IOException{
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(json, clazz);
}
MyCustomClass myCustomClass= new MyCustomClass();
myCustomClass= JsonUtil.deserialize(myCustomClass, MyCustomClass.class, json);
@JsonIgnoreProperties // ignores properties from json String which are not in your Pojo
public class MyCustomClass {
@JsonProperty("anotherNameIfFieldNameIsNotEqual")
private String picName;
private String picURL;
public String getPicName() {
return picName;
}
public void setPicName(String picName) {
this.picName = picName;
}
public String getPicURL() {
return picURL;
}
public void setPicURL(String picURL) {
this.picURL= picURL;
}
}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
这就是您需要的maven依赖关系:
public static <T> T deserialize(T t, Class<T> clazz, String json) throws JsonParseException, JsonMappingException, IOException{
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(json, clazz);
}
MyCustomClass myCustomClass= new MyCustomClass();
myCustomClass= JsonUtil.deserialize(myCustomClass, MyCustomClass.class, json);
@JsonIgnoreProperties // ignores properties from json String which are not in your Pojo
public class MyCustomClass {
@JsonProperty("anotherNameIfFieldNameIsNotEqual")
private String picName;
private String picURL;
public String getPicName() {
return picName;
}
public void setPicName(String picName) {
this.picName = picName;
}
public String getPicURL() {
return picURL;
}
public void setPicURL(String picURL) {
this.picURL= picURL;
}
}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
com.fasterxml.jackson.core
和并将为您完成所有这些
static class TestClass {
public int id;
public String name;
}
@Test
public void gson() {
Gson gson = new Gson();
TestClass[] item = gson.fromJson("[{'id': 1, 'name': 'testclass'}]", TestClass[].class);
assertThat(item[0].id, is(1));
assertThat(item[0].name, is("testclass"));
assertThat(item.length, is(1));
}
@Test
public void jackson() throws IOException {
ObjectMapper jacksonObjectMapepr = new ObjectMapper();
TestClass[] item = jacksonObjectMapepr.readValue("[{\"id\": 1, \"name\": \"testclass\"}]", TestClass[].class);
assertThat(item[0].id, is(1));
assertThat(item[0].name, is("testclass"));
assertThat(item.length, is(1));
}
然而,为了回答您的问题,您可以查找每个字段的内容,但是您必须做大量的工作来处理所有类型映射
@Test
public void sillyWayIDontRecommend() throws NoSuchFieldException, IllegalAccessException {
TestClass[] item = new TestClass[1];
JsonArray array = new JsonParser().parse("[{\"id\": 1, \"name\": \"testclass\"}]").getAsJsonArray();
for(int i = 0; i<array.size(); i++) {
item[i] = new TestClass();
JsonObject object = array.get(i).getAsJsonObject();
for(Map.Entry<String, JsonElement> entry : object.entrySet()) {
Field field = TestClass.class.getDeclaredField(entry.getKey());
if(field.getType().equals(int.class)) {
field.setInt(item[i], entry.getValue().getAsInt());
} else {
field.set(item[i], entry.getValue().getAsString());
}
}
}
assertThat(item[0].id, is(1));
assertThat(item[0].name, is("testclass"));
assertThat(item.length, is(1));
}
@测试
public void sillyWayIDontRecommend()不会引发此字段异常、非法访问异常{
TestClass[]项=新的TestClass[1];
JsonArray数组=新的JsonParser().parse(“[{\'id\':1,\'name\':\'testclass\'}]”)。getAsJsonArray();
对于(int i=0;i您可以使用Gson
库而不是json simple
Gson
可以将json字符串转换为Java对象,另一种方法是使用类似jackson
的json库吗?有了jackson,您可以用注释设置MyCustomClass
Pojo,以及应该解析哪些字段。您是否需要这些答案对你有帮助吗?