Import a related jar package
(1) First of all thank http://m.blog.csdn.net/blog/u013532827/19755907 to share, I directly use the Net.sf.json.* class packet
(2) packet download address: Http://download.csdn.net/detail/hedgehog8/4237121#comment inside the packet is still relatively full, and do not need to download points ~
(3) in the spirit of less than the introduction of the principle of jar bag, began only imported Json-lib-2.4-jdk15.jar, error: java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeExcept;
(4) Online check, said is also need to import
Commons-beanutils-1.7.0.jar
Commons-lang-2.1.jar
Ezmorph-1.0.2.jar
Continue importing after all import is still an error
Commons-collections-3.2.1.jar
Commons-logging-1.1.1.jar
And then it's okay.
2. Get the JSON data in the URL
public static string Loadjson (string url) {
StringBuilder json = new StringBuilder ();
try {
url urlobject = new URL (URL);
URLConnection UC = Urlobject.openconnection ();
BufferedReader in = new BufferedReader (New InputStreamReader (Uc.getinputstream (), "Utf-8"));
String inputline = null;
while ((Inputline = In.readline ())!= null) {
json.append (inputline);
}
In.close ();
} catch (Malformedurlexception e) {
e.printstacktrace ();
} catch (IOException e) {
e.printstacktrace (); c14/>} return
json.tostring ();
3. Get value based on key
After step 2, you can get the string of JSON, and the following will get value based on the key value
String URL = ""; Write your own URL link, I will not publish the HA ~ String json = Loadjson (URL); Gets the JSON string System.out.println (JSON);
Detects if Jsonobject Jsonobject=jsonobject.fromobject (JSON) is correctly obtained; System.out.println (Jsonobject.get ("id")); Where the ID is a key in JSON and detects whether the value can be obtained