首页 > 代码库 > 4.使用Jackson将Json数据转换成实体数据

4.使用Jackson将Json数据转换成实体数据

Jar下载地址:http://jackson.codehaus.org/


<script type="text/javascript" src="https://code.csdn.net/snippets/329454.js"></script><link rel="stylesheet" type="text/css" href="https://code.csdn.net/assets/embed-6889884a60106e973f6b1feb9c0a9986.css" media="screen">
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
package com.example.huang1;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
private TextView txtTest;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
txtTest=(TextView)findViewById(R.id.txtTest);
try {
String strJson="{\"studentList\":[{\"name\":\"Bob\", \"sex\":\"男\"},{\"name\":\"凯强\", \"sex\":\"女\"}]}";
ObjectMapper mObjectMapper=new ObjectMapper();
StudentList studentList=mObjectMapper.readValue(strJson, new TypeReference<StudentList>(){});
for(int i=0;i<studentList.getStudentList().size();i++)
{
txtTest.append(String.format("学生[%d]:姓名=%s 性别=%s\n",i, studentList.getStudentList().get(i).getName(),studentList.getStudentList().get(i).getSex()));
}
} catch (Exception e) {
txtTest.setText(e.getMessage());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}

}
来自CODE的代码片
MainActivity.java
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
package com.example.huang1;

public class Student {
public String name;
public String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
来自CODE的代码片
Student.java
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
package com.example.huang1;

import java.util.List;

public class StudentList {
public List<Student> studentList;

public List<Student> getStudentList() {
return studentList;
}

public void setStudentList(List<Student> studentList) {
this.studentList = studentList;
}

}
来自CODE的代码片
setStudentList.java


注意:
一、类中的属性名称一定要和Json数据的属性名称一致(大写和小写敏感),类之间的嵌套关系也应该和Json数据的嵌套关系一致。

二、当返回的json数据中新增了某些字段,而实体类又没有相关的属性名与之相应,此时的解决方式例如以下。即在类名上一行加入@JsonIgnoreProperties(ignoreUnknown = true)就可以。

@JsonIgnoreProperties(ignoreUnknown = true)//为了防止后台接口返回此类中未定义的属性而造成的bug。
public class CxcMoniJson extends SiteMoniJson {


}

三、当实体类中新增了某些属性,而返回的json数据又没有相关的字段与之相应,此时这样的情况是不会有bug的,放心使用。