如何从Internet获取JSON数组值

时间:2014-04-07 13:30:32

标签: arrays json

我想从互联网上的JSON代码中获取数组的值。来自此网址:http://olympics.clearlytech.com/api/v1/medals/

之后,我想显示我的脚本数组而不重写此URL上的JSON代码http://olympics.clearlytech.com/api/v1/medals/

那么,我可以使用哪些代码(脚本)?

例如,我想显示此数组的值

var JSONs = {
          example:['one','two','three']
};

代码是

document.write(JSONs.example[0]);

但如果我想从互联网获取数组值,我可以使用哪些代码/脚本?

3 个答案:

答案 0 :(得分:0)

使用jQuery,这是一个例子。在成功事件中,将生成的json文本转换为json对象。您还可以将内容类型设置为json,这样您就不必调用JSON.parse()

$.ajax({
        url: "http://olympics.clearlytech.com/api/v1/medals/",
        success: function(data) {
          var json = JSON.parse(data);
    }
});

这是另一种做同样的方式我希望你问如何解析每个值只是在jsfiddle中尝试这个

 $(document).ready(function(){
        alert("here");
    $.getJSON("http://olympics.clearlytech.com/api/v1/medals/",function(data){
           $.each(data,function(key,value){
               alert(data[key].country_name);
               alert(data[key].rank); 

               console.log(data[key].rank));
           });
       });    
    });

答案 1 :(得分:0)

public void handleResponse(String response) 

    {    
   //   display("Response:"+response);
        if(!response.equalsIgnoreCase(""))
        {
            JSONObject jso;
            try {
                jso = new JSONObject(response);


                    String status = jso.getString("status");
                    int valid=jso.getInt("valid");
              //     display("Welcome : "+UName);
                 if(valid>0)
                 {
                    if( status.equalsIgnoreCase("") || status==null ||  status.equalsIgnoreCase("Failed"))
                    {
                        invalid.setText("Invalid password");
                        //reset();
                        pwd.setText("");
                    }
                    else
                    {
                        //display(status);
                        intObj=new Intent(MainActivity.this,Design_Activity.class);
                        intObj.putExtra("Username", mUname);
                        startActivity(intObj);
                        MainActivity.this.finish();     
                    }

                 }
                 else
                 {
                     invalid.setText("Invalid userid");
                     uname.setText("");
                 }
                }
            catch (JSONException e1) {
                // TODO Auto-generated catch block
                Log.e(TAG, e1.getLocalizedMessage(), e1);
            }
            catch(Exception e)
            {
                Log.e(TAG, e.getLocalizedMessage(), e);
            }


        }
        else
        {
            display("Could not able to reach Server!");
        }


    }

答案 2 :(得分:-1)

虽然你希望我们做所有事情,这就是为什么你的问题变得消极。无论如何,你可以在普通的ajax中做到这一点

function getData(){
    // Initialize the Ajax request
    var xhr=new XMLHttpRequest();
    xhr.open('get', 'http://olympics.clearlytech.com/api/v1/medals/');

    // Track the state changes of the request
    xhr.onreadystatechange=function(){
        // Ready state 4 means the request is done
        if(xhr.readyState === 4){
        // 200 is a successful return
        if(xhr.status === 200){
            alert(xhr.responseText); // 'This is the returned text.'
        }else{
            alert('Error: '+xhr.status); // An error occurred during the request
        }
        }
    }

    // Send the request to send-ajax-data.php
    xhr.send(null);
}