1 🎜>2. Tukar Json kepada entiti
JSONObject jsonObject = JSONObject.fromObject(jsonStr); Map<String,Object> map = new HashMap<>(); map.put("code",jsonObject .getInt("code"));
Jika entiti itu mempunyai medan Senarai, anda perlu menentukan
JSONObject jsonObject = JSONObject.fromObject(jsonStr); ArticleForm articleForm = (ArticleForm) JSONObject.toBean(jsonObject , ArticleForm.class);
3 satu:
Map<String, Class> classMap = new HashMap<String, Class>(); classMap.put("keywords", String.class); ArticleForm articleForm = (ArticleForm) JSONObject.toBean(data.getJSONObject(i), ArticleForm.class,classMap);
2. com.alibaba.fastjson
1 Tukarkan Json kepada Peta
List<ArticleForm> list = new ArrayList<>(); JSONArray data = jsonObject.getJSONArray("data"); if (errorCode == 0 && data != null && !data.isEmpty()) { for (int i = 0; i < data.size(); i++) { Map<String, Class> classMap = new HashMap<String, Class>(); classMap.put("keywords", String.class); ArticleForm articleForm = (ArticleForm) JSONObject.toBean(data.getJSONObject(i), ArticleForm.class,classMap); list.add(articleForm); } }
List<ArticleForm> list = new ArrayList<>(); JSONArray data = jsonObject.getJSONArray("data"); if (errorCode == 0 && data != null && !data.isEmpty()) { Map<String, Class> classMap = new HashMap<String, Class>(); classMap.put("keywords", String.class); list = (List<ArticleForm>) JSONArray.toArray(data, ArticleForm.class,classMap); }
3. Tukar Json kepada koleksi
JSONObject jsonObject = JSON.parseObject(jsonStr); Map<String,Object> map = new HashMap<>(); map.put("code",jsonObject .getInt("code"));
Pemprosesan json yang biasa digunakan dalam Java
ArticleForm articleForm = JSON.parseObject(jsonStr, new TypeReference<ArticleForm>() {});
Atas ialah kandungan terperinci Apakah kaedah pemprosesan Json di Jawa?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!