Gson(又称Google Gson)是Google公司发布的一个开放源代码的Java库,主要用途为序列化Java对象为JSON字符串,或反序列化JSON字符串成Java对象
添加依赖包 项目结构管理–>依赖–>Library Dependency–>搜索Gson(com.google.code.gson:gson:2.8.0)
安装Json快速生成实体类插件 Setting–>plugins–>Browse Repositories–>GsonFormat
设置快捷键 Setting–>Keymap–>search–>gson
使用 在代码编辑区使用快捷键,然后输入JSON文档,就可以生成JSON实体类了
具体实现代码 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 public class MainActivity extends AppCompatActivity { @BindView (R.id.edit_query) EditText mEditQuery; @BindView (R.id.tv_result) TextView mTvResult; @Override protected void onCreate (Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this ); } public void search (View view) { final String search_content = mEditQuery.getText().toString(); new AsyncTask<String, Void, String>() { @Override protected String doInBackground (String... params) { try { URL url = new URL(params[0 ]); URLConnection connection = url.openConnection(); InputStream input = connection.getInputStream(); InputStreamReader reader = new InputStreamReader(input); BufferedReader bufferedReader = new BufferedReader(reader); StringBuilder builder = new StringBuilder(); String line; while ((line = bufferedReader.readLine()) != null ) { builder.append(line); } return builder.toString(); } catch (IOException e) { e.printStackTrace(); } return null ; } @Override protected void onPostExecute (String s) { String result = "" ; Gson gson = new Gson(); YouDao youDao = gson.fromJson(s, YouDao.class ) ; if (RegularExpression.isChinese(search_content)) { result = youDao.getTranslation().get(0 ); } else { String temp = "\n" + "webTranslation:" + "\n" ; List<YouDao.WebBean> webList = youDao.getWeb(); for (int i=0 ;i <webList.size(); i++){ temp += webList.get(i).toString() + "\n" ; } result = youDao.getBasic().toString() + temp; } mTvResult.setText(result); super .onPostExecute(s); } }.execute("http://fanyi.youdao.com/openapi.do?keyfrom=" + "FloatingSearchApi&key" + "=598006220&type=data&doctype=" + "json&version=1.1&q=" + search_content); } }
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 package www.lollipopstudio.top.gsontest;import com.google.gson.annotations.SerializedName;import java.util.List;public class YouDao { private BasicBean basic; private String query; private int errorCode; private List<String> translation; private List<WebBean> web; public BasicBean getBasic () { return basic; } public void setBasic (BasicBean basic) { this .basic = basic; } public String getQuery () { return query; } public void setQuery (String query) { this .query = query; } public int getErrorCode () { return errorCode; } public void setErrorCode (int errorCode) { this .errorCode = errorCode; } public List<String> getTranslation () { return translation; } public void setTranslation (List<String> translation) { this .translation = translation; } public List<WebBean> getWeb () { return web; } public void setWeb (List<WebBean> web) { this .web = web; } public static class BasicBean { @SerializedName ("us-phonetic" ) private String usphonetic; private String phonetic; @SerializedName ("uk-phonetic" ) private String ukphonetic; private List<String> explains; public String getUsphonetic () { return usphonetic; } public void setUsphonetic (String usphonetic) { this .usphonetic = usphonetic; } public String getPhonetic () { return phonetic; } public void setPhonetic (String phonetic) { this .phonetic = phonetic; } public String getUkphonetic () { return ukphonetic; } public void setUkphonetic (String ukphonetic) { this .ukphonetic = ukphonetic; } public List<String> getExplains () { return explains; } public void setExplains (List<String> explains) { this .explains = explains; } @Override public String toString () { String result = "" ; for (int i = 0 ; i < getExplains().size(); i++) { if (i < getExplains().size() - 1 ) { result += getExplains().get(i) + "\n" ; } else { result += getExplains().get(i); } } return "en:" + getUkphonetic() + "\n" + "us:" + getUsphonetic() + "\n" + "Translation:" + "\n" + result; } } public static class WebBean { private String key; private List<String> value; public String getKey () { return key; } public void setKey (String key) { this .key = key; } public List<String> getValue () { return value; } public void setValue (List<String> value) { this .value = value; } @Override public String toString () { String result = "" ; for (int i = 0 ; i < getValue().size(); i++) { if (i < getValue().size() - 1 ) { result += getValue().get(i) + "," ; } else { result += getValue().get(i); } } return getKey() + ":" + result; } } }