特殊JSON的解析————有点没搞太明白

    技术2024-11-16  19

    JSON源码{ "code": 0, "list": { "0": { "aid": "6008965", "author": "哔哩哔哩番剧", "coins": 170, "copyright": "Copy", "create": "2016-08-25 21:34" }, "1": { "aid": "6008938", "author": "哔哩哔哩番剧", "coins": 404, "copyright": "Copy", "create": "2016-08-25 21:33" } } }

    JSON里{}代表一个对象(类)[]代表一个数组,因此我们可以这样理解json对象,在一个封装的业务类里,有code这个变量,之后还有list这个内部类 public class json extends AppCompatActivity { private Button json; private TextView textjson; private Button json2; private JSONObject jsonObject; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_json); json = findViewById(R.id.json); textjson = findViewById(R.id.textjson); json2 = findViewById(R.id.json2); final String jsons2="{\n" + " \"code\": 0,\n" + " \"list\": {\n" + " \"0\": {\n" + " \"aid\": \"6008965\",\n" + " \"author\": \"哔哩哔哩番剧\",\n" + " \"coins\": 170,\n" + " \"copyright\": \"Copy\",\n" + " \"create\": \"2016-08-25 21:34\"\n" + " },\n" + " \"1\": {\n" + " \" aid\": \"6008938\",\n" + " \"author\": \"哔哩哔哩番剧\",\n" + " \"coins\": 404,\n" + " \"copyright\": \"Copy\",\n" + " \"create\": \"2016-08-25 21:33\"\n" + " }\n" + " }\n" + "}"; final String jsons=" {\"temp\": \"20℃/30℃\",\"weather\": \"晴转多云\",\"name\": \"上海\",\"wind\": \"1级\"}"; json.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { jsonObject = new JSONObject(jsons); String id=jsonObject.getString("temp"); String id1=jsonObject.getString("weather"); jsons jsons1=new jsons(id,id1); textjson.setText(jsons1.toString()); } catch (JSONException e) { e.printStackTrace(); } } }); json2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { jsonspli jsonspli = new jsonspli(); try { jsonObject=new JSONObject(jsons2); int code = jsonObject.optInt("code"); JSONObject list=jsonObject.optJSONObject("list"); jsonspli.setCode(code); List<jsonspli.filmbean> lists=new ArrayList<>(); jsonspli.setList(lists); for (int i=0;i<list.length();i++) { JSONObject jsonObject1=list.optJSONObject( i +""); if(jsonObject1 !=null) { String aid=jsonObject1.optString("aid"); String author=jsonObject1.optString("author"); int coins = jsonObject1.optInt("coins"); String copyright = jsonObject1.optString("copyright"); String create = jsonObject1.optString("create"); jsonspli.filmbean filmbean=new jsonspli.filmbean(); filmbean.setAid(aid); filmbean.setAuthor(author); filmbean.setCoins(coins); filmbean.setCopyright(copyright); filmbean.setCreate(create); lists.add(filmbean); } } } catch (JSONException e) { e.printStackTrace(); } textjson.setText(jsonspli.toString()); } }); } }

    这种情况是针对于Gson无法解析的,可以看到0和1这两个类,是不能封装成0和1 类名的,数字不能作为类名的起点,所以看到,list就是一个傀儡了,在java后期的代码中,list进去之后,就开始直接对1和0两个对象进行操作了。 有点明白了!是他将整个对象封装成了一个数组了,整个业务类来看,只有data和一个含有内部类泛型的list,只有的话再输出一定是一个含有多个内部类的了,具体都是在java代码里添加的 public class jsonspli { private int code; private List list; public int getCode() { return code; }

    public void setCode(int code) { this.code = code; } @Override public String toString() { return "jsonspli{" + "code=" + code + ", list=" + list + '}'; } public List<filmbean> getList() { return list; } public void setList(List<filmbean> list) { this.list = list; } public static class filmbean{ private String aid; private String author; private int coins; private String copyright; private String create; @Override public String toString() { return "filmbean{" + "aid='" + aid + '\'' + ", author='" + author + '\'' + ", coins=" + coins + ", copyright='" + copyright + '\'' + ", create='" + create + '\'' + '}'; } public String getAid() { return aid; } public void setAid(String aid) { this.aid = aid; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public int getCoins() { return coins; } public void setCoins(int coins) { this.coins = coins; } public String getCopyright() { return copyright; } public void setCopyright(String copyright) { this.copyright = copyright; } public String getCreate() { return create; } public void setCreate(String create) { this.create = create; } }

    }

    Processed: 0.009, SQL: 9