1 2 3 4 5 6 7 8 9 10 | import com.google.gwt.json.client.* JSONObject jsonObject = new JSONObject(); JSONString value = new JSONString("yuexiaopeng"); jsonObject.put("name", value); JSONArray arrayValue = new JSONArray(); arrayValue.set(0, new JSONString("item0")); arrayValue.set(1, new JSONString("item1")); jsonObject.put("array", arrayValue); |
1 | {name: "yuexiaopeng", array: {"item", "item"}} |
1 2 3 4 5 6 7 8 9 10 | import org.json.* JSONObject jsonObject = new JSONObject(jsonBody); String name = jsonObject.getString("name"); System.out.println("name is:" + name); JSONArray jsonArray = jsonObject.getJSONArray("array"); for (int i = 0; i < jsonArray.length(); i++) { System.out.println("item " + i + " :" + jsonArray.getString(i)); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import org.w3c.dom.* Document xmlDoc = new DocumentImpl(); // create the root element Element rootElement = xmlDoc.createElement("employees"); Element employeeItem = xmlDoc.createElement("employee"); employeeItem.setAttribute("ID", "0008"); Element Name = xmlDoc.createElement("name"); Name.appendChild(xmlDoc.createTextNode("yuexiaopeng")); employeeItem.appendChild(Name); rootElement.appendChild(employeeItem); xmlDoc.appendChild(rootElement); //convert XML object to string Document document = rootElement.getOwnerDocument(); DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation(); LSSerializer serializer = domImplLS.createLSSerializer(); String result = serializer.writeToString(document); |
1 2 3 4 5 6 7 | <employees> <employee ID = "0008"> <name> yuexiaopeng </name> </employee> </employees> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import com.google.gwt.xml.client.* Document employeeDom = XMLParser.parse(xmlText); Element employeeElement = employeeDom.getDocumentElement(); XMLParser.removeWhitespace(employeeElement); NodeList employees = employeeElement.getElementsByTagName("employee"); Element employee = (Element) employees.item(0); NamedNodeMap attributes = employee.getAttributes(); Node attribute = attributes.getNamedItem("ID"); String ID = attribute.getNodeValue(); String name = employee.getElementsByTagName("name").item(0).getFirstChild() .getNodeValue(); |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |