好的,如果有人提出这个问题,我真的很抱歉,但我似乎无法找到任何好的帮助。
我使用PHP和Codeigniter为我的Android应用程序编写了一个小API。它通过GET请求工作,所以我会发送一些信息,它会返回一些东西。当我向其发送登录请求时,我希望它验证用户数据,如果正确则返回用户的凭据(uid,电子邮件等)。所有这些信息都可以作为格式化XML或JSON数组返回。我的问题是在Java中更容易使用,你能帮我一个简单的例子吗?
非常感谢任何帮助。
编辑:
请求的代码
// Parse JSON array
JSONObject obj = (JSONObject) JSONValue.parse(response);
String uid = (String) obj.get("uid");
String email = (String) obj.get("email");
答案 0 :(得分:3)
根据我的经验,JSON更容易使用。
这是一些用XML格式化的数据:
<book>
<person>
<first>Kiran</first>
<last>Pai</last>
<age>22</age>
</person>
<person>
<first>Bill</first>
<last>Gates</last>
<age>46</age>
</person>
<person>
<first>Steve</first>
<last>Jobs</last>
<age>40</age>
</person>
</book>
以下是读取XML文件的示例代码:
import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class ReadAndPrintXMLFile{
public static void main (String argv []){
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("book.xml"));
// normalize text representation
doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is " +
doc.getDocumentElement().getNodeName());
NodeList listOfPersons = doc.getElementsByTagName("person");
int totalPersons = listOfPersons.getLength();
System.out.println("Total no of people : " + totalPersons);
for(int s=0; s<listOfPersons.getLength() ; s++){
Node firstPersonNode = listOfPersons.item(s);
if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){
Element firstPersonElement = (Element)firstPersonNode;
//-------
NodeList firstNameList = firstPersonElement.getElementsByTagName("first");
Element firstNameElement = (Element)firstNameList.item(0);
NodeList textFNList = firstNameElement.getChildNodes();
System.out.println("First Name : " +
((Node)textFNList.item(0)).getNodeValue().trim());
//-------
NodeList lastNameList = firstPersonElement.getElementsByTagName("last");
Element lastNameElement = (Element)lastNameList.item(0);
NodeList textLNList = lastNameElement.getChildNodes();
System.out.println("Last Name : " +
((Node)textLNList.item(0)).getNodeValue().trim());
//----
NodeList ageList = firstPersonElement.getElementsByTagName("age");
Element ageElement = (Element)ageList.item(0);
NodeList textAgeList = ageElement.getChildNodes();
System.out.println("Age : " +
((Node)textAgeList.item(0)).getNodeValue().trim());
//------
}//end of if clause
}//end of for loop with s var
}catch (SAXParseException err) {
System.out.println ("** Parsing error" + ", line "
+ err.getLineNumber () + ", uri " + err.getSystemId ());
System.out.println(" " + err.getMessage ());
}catch (SAXException e) {
Exception x = e.getException ();
((x == null) ? e : x).printStackTrace ();
}catch (Throwable t) {
t.printStackTrace ();
}
//System.exit (0);
}//end of main
}
这是用JSON格式化的相同数据:
{"book":
[
{"person":
{"last":"Pai","age":22,"first":"Kiran"}
},
{"person":
{"last":"Gates","age":46,"first":"Bill"}
},
{"person":
{"last":"Jobs","age":40,"first":"Steve"}
}
]
}
以及读取JSON的示例代码:
JSONObject obj = (JSONObject) JSONValue.parse(jsonString);
JSONArray arr = (JSONArray) obj.get("book");
for (int i = 0; i < arr.size(); i++) {
JSONObject person = (JSONObject) arr.get(i);
String first = (String) person.get("first");
String last = (String) person.get("last");
String age = (String) person.get("age");
System.out.println("Person " + i +" : first = "+ first + ", last = " + last + ", age = " + age);
}
从2个示例代码中,您可以看到JSON更易于阅读,编写和格式化。
XML更常用作交换信息的行业标准,而JSON通常用于通过基于REST的API交换信息。