Java get JSON data in URL __ frame

Source: Internet
Author: User
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 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.