闲话少扯,直接代码附上:
private String id,name,code,type,img,value; /* //解析第一条数据 private String jsonData="<Data message=\'\' jsondata=\'{\"ds\":[{\"ID\":\"1\",\"Name\":\"智能控制物联网\",\"IPAddress\":\"192.16" + "8.0.0\"}]}\'/>"*/; //解析第二条数据 private String jsonData = "<Data message=\'\' jsondata=\'{\"ds\":[{\"ID\":\"1\",\"Code\":\"1\",\"Name\":\"灯光\",\"HDType\":\"1\",\"Img\":\"Devices/01.png\",\"IndexValue\":\"1\"},{\"ID\":\"2\",\"Code\":\"2\",\"Name\":\"窗帘\",\"HDType\":\"2\",\"Img\":\"Devices/05.png\",\"IndexValue\":\"2\"},{\"ID\":\"3\",\"Code\":\"3\",\"Name\":\"可调灯光\",\"HDType\":\"3\",\"Img\":\"Devices/02.png\",\"IndexValue\":\"3\"},{\"ID\":\"4\",\"Code\":\"4\",\"Name\":\"开窗器\",\"HDType\":\"4\",\"Img\":\"Devices/06.png\",\"IndexValue\":\"4\"},{\"ID\":\"5\",\"Code\":\"5\",\"Name\":\"电子门锁\",\"HDType\":\"5\",\"Img\":\"Devices/07.png\",\"IndexValue\":\"5\"},{\"ID\":\"6\",\"Code\":\"6\",\"Name\":\"插座\",\"HDType\":\"1\",\"Img\":\"Devices/16.png\",\"IndexValue\":\"6\"},{\"ID\":\"7\",\"Code\":\"7\",\"Name\":\"电视\",\"HDType\":\"901\",\"Img\":\"Devices/03.png\",\"IndexValue\":\"7\"},{\"ID\":\"8\",\"Code\":\"8\",\"Name\":\"空调\",\"HDType\":\"901\",\"Img\":\"Devices/04.png\",\"IndexValue\":\"8\"},{\"ID\":\"19\",\"Code\":\"17\",\"Name\":\"摄像监控\",\"HDType\":\"902\",\"Img\":\"Devices/09.png\",\"IndexValue\":\"9\"},{\"ID\":\"9\",\"Code\":\"9\",\"Name\":\"温度感应器\",\"HDType\":\"6\",\"Img\":\"Devices/19.png\",\"IndexValue\":\"10\"},{\"ID\":\"10\",\"Code\":\"10\",\"Name\":\"湿度感应器\",\"HDType\":\"7\",\"Img\":\"Devices/12.png\",\"IndexValue\":\"11\"},{\"ID\":\"11\",\"Code\":\"11\",\"Name\":\"电压感应器\",\"HDType\":\"8\",\"Img\":\"Devices/18.png\",\"IndexValue\":\"12\"},{\"ID\":\"12\",\"Code\":\"12\",\"Name\":\"电流感应器\",\"HDType\":\"9\",\"Img\":\"Devices/17.png\",\"IndexValue\":\"13\"},{\"ID\":\"13\",\"Code\":\"13\",\"Name\":\"烟雾感应器\",\"HDType\":\"10\",\"Img\":\"Devices/10.png\",\"IndexValue\":\"14\"},{\"ID\":\"14\",\"Code\":\"14\",\"Name\":\"人体感应器\",\"HDType\":\"10\",\"Img\":\"Devices/13.png\",\"IndexValue\":\"15\"},{\"ID\":\"15\",\"Code\":\"15\",\"Name\":\"燃气感应器\",\"HDType\":\"10\",\"Img\":\"Devices/10.png\",\"IndexValue\":\"16\"}]}\'/>"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* //解析第一条数据 String sJson = jsonData.substring(33); Log.d("MainActivity", sJson); try { JsonReader reader = new JsonReader(new StringReader(sJson)); reader.beginArray(); while (reader.hasNext()) { reader.beginObject(); while (reader.hasNext()) { String tagName = reader.nextName(); if (tagName.equals("ID")) { System.out.println(reader.nextString()); } else if (tagName.equals("Name")) { System.out.println(reader.nextString()); } else if (tagName.equals("IPAddress")) { System.out.println(reader.nextString()); } } reader.endObject(); } reader.endArray(); } catch (Exception e) { e.printStackTrace(); }*/ //解析第二条数据 Map< String, String> map = new HashMap<String, String>(); String sJson = jsonData.substring(33); Log.d("MainActivity", sJson); try { JsonReader reader = new JsonReader(new StringReader(sJson)); reader.beginArray(); while (reader.hasNext()) { reader.beginObject(); while (reader.hasNext()) { String tagName = reader.nextName(); if (tagName.equals("ID")) { id = reader.nextString(); } else if (tagName.equals("Code")) { code = reader.nextString(); } else if (tagName.equals("Name")) { name = reader.nextString(); } else if (tagName.equals("HDType")) { type = reader.nextString(); } else if (tagName.equals("Img")) { img=reader.nextString(); } else if (tagName.equals("IndexValue")) { value = reader.nextString(); } } map.put("ID", id); map.put("Code",code); map.put("Name", name); map.put("HDType", type); map.put("Img", img); map.put("IndexValue",value); reader.endObject(); } reader.endArray(); } catch (Exception e) { e.printStackTrace(); } }