谷歌阿比解析的JSON [英] Google Api Parsing in json

查看:98
本文介绍了谷歌阿比解析的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能以JSON格式解析这个网址是什么?

https://开头阿贾克斯.googleapis.com / AJAX /服务/搜索/ WEB V = 1.0安培; Q =&放大器; RSZ = 8

 公共类MainActivity扩展ListActivity {
    私人上下文的背景下;
    私有静态字符串URL =htt​​ps://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=pixelloid&rsz=8;

    私有静态最后弦乐TAG_VTYPE =responseData;
    私有静态最后弦乐TAG_VCOLOR =结果;
    私有静态最后弦乐TAG_FUEL =GsearchResultClass;
    私有静态最后弦乐TAG_TREAD =URL;
    私有静态最后弦乐TAG_OPERATOR =URL;
    私有静态最后弦乐TAG_NAME =名;
    私有静态最后弦乐TAG_POINTS =experiencePoints;

    ArrayList的< HashMap的<字符串,字符串>> jsonlist =新的ArrayList< HashMap的<字符串,字符串>>();

    ListView的LV;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        新ProgressTask(MainActivity.this).execute();
    }

    私有类ProgressTask扩展的AsyncTask<字符串,太虚,布尔> {
        私人ProgressDialog对话框;

        私人ListActivity的活动;

        //私有列表<消息>消息;
        公共ProgressTask(ListActivity活动){
            this.activity =活动;
            上下文=活动;
            对话框=新ProgressDialog(上下文);
        }

        / **进度对话框显示用户的备份处理。 * /

        / **应用程序上下文。 * /
        私人上下文的背景下;

        在preExecute保护无效(){
            this.dialog.setMessage(进度开始);
            this.dialog.show();
        }

        @覆盖
        保护无效onPostExecute(最终布尔成功){
            如果(dialog.isShowing()){
                dialog.dismiss();
            }
            ListAdapter适配器=新SimpleAdapter(背景下,jsonlist,
                    R.layout.list_item,新的String [] {TAG_VTYPE,TAG_VCOLOR,
                            TAG_FUEL,TAG_TREAD},新的INT [] {
                            R.id.vehicleType,R.id.vehicleColor,R.id.fuel,
                            R.id.treadType});

            setListAdapter(适配器);

            //选择单一的ListView项
             LV = getListView();






        }

        保护布尔doInBackground(最后的字符串参数... args){

            JSONParser jParser =新JSONParser();

            //从URL获取JSON字符串
            JSONArray JSON = jParser.getJSONFromUrl(URL);

            如果(JSON == NULL){
                   //通知用户
                Log.e(==>中,无法下载文件);

            }
             其他 {
            的for(int i = 0; I< json.length();我++){

                尝试 {
                    JSONObject的C = json.getJSONObject(我);
                    字符串由Vtype = c.getString(TAG_VTYPE);

                    字符串vcolor = c.getString(TAG_VCOLOR);
                    字符串vfuel = c.getString(TAG_FUEL);
                    字符串vtread = c.getString(TAG_TREAD);

                    HashMap的<字符串,字符串>图=新的HashMap<字符串,字符串>();

                    //添加每个子节点HashMap中的key =>值
                    map.put(TAG_VTYPE,Vtype域);
                    map.put(TAG_VCOLOR,vcolor);
                    map.put(TAG_FUEL,vfuel);
                    map.put(TAG_TREAD,vtread);
                    jsonlist.add(图)
                }赶上(JSONException E){
                    // TODO自动生成的catch块
                    e.printStackTrace();
                    Log.e(==>中,iyogbr克; UI);

                }
            }
             }

            返回null;
        }

    }

}
 

我解析这些数据:

  {responseData:{结果:[{内容:&LT; B&GT; Pixelloid&LT; \ / B&GT;工作室的视觉效果,3D动画制作和培训机构。 ,GsearchResultClass:GwebSearch,titleNoFormatting:Pixelloid  - 视觉效果,3D动画 - 生产和培训,称号:&LT; B&GT; Pixelloid&LT; \ / B&GT;  - 视觉特效,3D动画 - 生产Training","cacheUrl":"http:\/\/www.google.com\/search?q=cache:Uq3_USWdbwIJ:www.pixelloid.com","unescapedUrl":"http:\/\/www.pixelloid.com\/","url":"http:\/\/www.pixelloid.com\/","visibleUrl":"www.pixelloid.com"},{"content":"Pixelloid工作室的视觉效果,3D动画制作和培训与LT; B&GT; ...&LT; \ / B&gt;中,GsearchResultClass:GwebSearch,titleNoFormatting:生产和培训学院 -  Pixelloid,称号:生产和培训学院 - <b>Pixelloid<\/b>","cacheUrl":"http:\/\/www.google.com\/search?q=cache:Pce2UIgSVDoJ:www.pixelloid.com","unescapedUrl":"http:\/\/www.pixelloid.com\/training\/","url":"http:\/\/www.pixelloid.com\/training\/","visibleUrl":"www.pixelloid.com"},{"content":"<b>Pixelloid<\/b>是一个屡获殊荣的3D动画和放大器;放大器;视觉效果制作和培训工作室设在海得拉巴在班加罗尔,维萨卡帕​​特南&LT分支机构; B&GT; ...&LT; \ / B&gt;中,GsearchResultClass:GwebSearch,titleNoFormatting :Pixelloid工作室 - 的YouTube,称号:&LT; B&GT; Pixelloid&LT; \ / B&GT;工作室 - YouTube","cacheUrl":"http:\/\/www.google.com\/search?q=cache:wrfWBrpn45AJ:www.youtube.com","unescapedUrl":"http:\/\/www.youtube.com\/user\/pixelloid","url":"http:\/\/www.youtube.com\/user\/pixelloid","visibleUrl":"www.youtube.com"},{"content":"<b>Pixelloid<\/b>, ,海得拉巴,印度5896喜欢·60谈论这个·408在这里我们帮助人们有效地讲述自己的故事,我们做动画,为&LT的视觉效果; B&GT; ...&LT; \ / B&gt;中,GsearchResultClass。: GwebSearch,titleNoFormatting:Pixelloid  - 海得拉巴,印度 - 媒体\ /新闻\ /出版|脸谱,称号:&LT; B&GT; Pixelloid&LT; \ / B&GT;  - 海得拉巴,印度 - 媒体\ /新闻\ /出版| Facebook","cacheUrl":"http:\/\/www.google.com\/search?q=cache:_m-PNJ9c3sgJ:www.facebook.com","unescapedUrl":"https:\/\/www.facebook.com\/Pixelloid","url":"https:\/\/www.facebook.com\/Pixelloid","visibleUrl":"www.facebook.com"},{"content":"Welcome以&LT公司概况; B&GT; Pixelloid&LT; \ / B&GT;工作室私人有限公司在LinkedIn&LT; B&GT; Pixelloid&LT; \ / B&GT;是一个屡获与活动&LT获奖内容制作公司; B&GT; ...&LT; \ / B&gt;中,GsearchResultClass:GwebSearch,titleNoFormatting:Pixelloid工作室私人有限公司| LinkedIn,称号:&LT; B&GT; Pixelloid&LT; \ / B&GT;工作室私人有限公司| LinkedIn","cacheUrl":"http:\/\/www.google.com\/search?q=cache:vnyg3sUrGfgJ:www.linkedin.com","unescapedUrl":"http:\/\/www.linkedin.com\/company\/pixelloid-studios-private-limited","url":"http:\/\/www.linkedin.com\/company\/pixelloid-studios-private-limited","visibleUrl":"www.linkedin.com"},{"content":"The最新来自以下; B&GT; Pixelloid&LT; \ / B&GT;(@&LT; B&GT; Pixelloid&LT; \ / B&GT;)&LT; B&GT; Pixelloid&LT; \ / B&GT;是一个屡获殊荣的3D动画和放大器;放大器;视觉效果工作室与一个专门的商业培训师,GsearchResultClass:GwebSearch,titleNoFormatting:Pixelloid(Pixelloid)的Twitter,称号:&LT; B&GT; Pixelloid&LT; \ / B&GT;(小于B&GT; Pixelloid&LT。 ; \ / B&GT;)上Twitter","cacheUrl":"http:\/\/www.google.com\/search?q=cache:s4gJgJcovCsJ:twitter.com","unescapedUrl":"https:\/\/twitter.com\/Pixelloid","url":"https:\/\/twitter.com\/Pixelloid","visibleUrl":"twitter.com"},{"content":"Feb 10,到2013年&LT; B&GT; ...&LT; \ / B&GT;&LT; B&GT; Pixelloid&LT; \ / B&gt;中海得拉巴是印度唯一的培训机构,有一个活跃的生产环境和LT; B&GT; Pixelloid和放大器;#39 ; S&LT; \ / B&GT;课程针对的&lt; B&GT; ...&LT; \ / B&gt;中,GsearchResultClass:GwebSearch,titleNoFormatting:Pixelloid  - 入学,课程,学费,地址| Pixelloid |海得拉巴......,称号:&LT; B&GT; Pixelloid&LT; \ / B&GT;  - 入学,课程,学费,地址|&LT; B&GT; Pixelloid&LT; \ / B&GT; |海得拉巴<b>...<\/b>","cacheUrl":"http:\/\/www.google.com\/search?q=cache:3OWFak05f30J:www.minglebox.com","unescapedUrl":"http:\/\/www.minglebox.com\/college\/pixelloid-Hyderabad","url":"http:\/\/www.mi
 

解决方案

您是误会了JSON序列化方式的对象。基本上,(非常基本) {...} 是对象, [...,...] 是数组和键:值是对象的属性,所以是这样的:

  {
  responseData:{
     结果:[],
     光标: {
         moreResultsUrl:http://www.url.com,
         searchResultTime:0,00
     }
  },
  responseDetails:空,
  responseStatus:200
}
 

意味着你必须有3个属性的第一个目的: responseData (对象), responseDetails (null对象)和 responseStatus (INT)。而 responseData 是一个对象,具有2个属性:结果(阵列),光标(对象)。而光标的属性 moreResultsUrl (串),而 searchResultTime (串)。

现在,来解析JSON,你可以尝试这样的:

  • 您可以在您的code提供太克隆 - 从-教程头脑。你将不得不修改的东西多一点,以保持一定的等级参数命名的(你的变量谈论车和你对网络搜索的数据...)。你可以重命名ListAdapter适配器=新的简单的... 的方法,以满足您的需求,从的一些数据。

**为了使用这些JSON方法,你需要包含简单的JSON库。

 进口java.text.ParseException;
进口的java.util.ArrayList;
进口的java.util.HashMap;

进口org.json.JSONArray;
进口org.json.JSONException;
进口org.json.JSONObject;

进口android.app.ListActivity;
进口android.app.ProgressDialog;
进口android.content.Context;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.widget.ListAdapter;
进口android.widget.SimpleAdapter;

公共类MainActivity扩展ListActivity {

私人上下文的背景下;
私有静态字符串URL =htt​​ps://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=pixelloid&rsz=8;

私有静态最后弦乐TAG_content =内容;
私有静态最后弦乐TAG_GsearchResultClass =GsearchResultClass;
私有静态最后弦乐TAG_titleNoFormatting =titleNoFormatting;
私有静态最后弦乐TAG_title =称号;
私有静态最后弦乐TAG_cacheUrl =cacheUrl;
私有静态最后弦乐TAG_unescapedUrl =unes​​capedUrl;
私有静态最后弦乐TAG_url =URL;
私有静态最后弦乐TAG_visibleUrl =visibleUrl;



@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);

    新ProgressTask(MainActivity.this).execute();
}

私有类ProgressTask扩展的AsyncTask&LT;字符串,太虚,布尔&GT; {
    私人ProgressDialog对话框;

    私人ListActivity的活动;

    //私有列表&LT;消息&GT;消息;
    公共ProgressTask(ListActivity活动){
        this.activity =活动;
        上下文=活动;
        对话框=新ProgressDialog(上下文);
    }

    / **进度对话框显示用户的备份处理。 * /

    / **应用程序上下文。 * /
    私人上下文的背景下;

    在preExecute保护无效(){
        this.dialog.setMessage(进度开始);
        this.dialog.show();
    }

    @覆盖
    保护无效onPostExecute(最终布尔成功){
        如果(dialog.isShowing()){
            dialog.dismiss();
        }
        ListAdapter适配器=新SimpleAdapter(背景,内容,R.layout.list_item,
                                    新的String [] {TAG_title,TAG_content,TAG_url},
                                    新的INT [] {R.id.vehicleType,R.id.vehicleColor,R.id.fuel,R.id.treadType});

        setListAdapter(适配器);

        //选择单一的ListView项
        // LV = getListView();
    }

    保护布尔doInBackground(最后的字符串参数... args){
            loadJson();
            返回null;
    }
}

公共静态的JSONObject解析(){
    JSONParser分析器=新JSONParser();
    obj对象;
    JSONObject的JSON =新的JSONObject();
    尝试 {
        OBJ = parser.getJSONFromUrl(URL);
        JSON =(JSONObject的)目标文件;
    }赶上(ParseException的E){
        e.printStackTrace();
    }

    返回JSON;
}

私人字符串moreResultsUrl =;
私人的ArrayList&LT; HashMap的&LT;字符串,字符串&GT;&GT;内容;

公共无效loadJson(){

    尝试 {
        JSONObject的OBJ =解析();
        如果(OBJ!= NULL){
             的JSONObject responseData;
             responseData =(的JSONObject)obj.get(responseData);

             如果(responseData!= NULL){
                JSONArray结果=(JSONArray),其中obj.get(结果);
                内容=新的ArrayList&LT; HashMap的&LT;字符串,字符串&GT;&GT;();

                HashMap的&LT;字符串,字符串&GT;地图;
                JSONObject的CRO;
                的for(int i = 0; I&LT; results.length();我++){
                    图=新的HashMap&LT;字符串,字符串&GT;();
                    CRO =(的JSONObject)results.get(我);

                    map.put(TAG_content,cro.get(TAG_content)的ToString());
                    map.put(TAG_GsearchResultClass,cro.get(TAG_GsearchResultClass)的ToString());
                    map.put(TAG_titleNoFormatting,cro.get(TAG_titleNoFormatting)的ToString());
                    map.put(TAG_titleNoFormatting,cro.get(TAG_titleNoFormatting)的ToString());
                    map.put(TAG_title,cro.get(TAG_title)的ToString());
                    map.put(TAG_cacheUrl,cro.get(TAG_cacheUrl)的ToString());
                    map.put(TAG_unescapedUrl,cro.get(TAG_unescapedUrl)的ToString());
                    map.put(TAG_url,cro.get(TAG_url)的ToString());
                    map.put(TAG_visibleUrl,cro.get(TAG_visibleUrl)的ToString());

                    contents.add(图)
                }

                JSONObject的光标=(的JSONObject)responseData.get(光标);
                如果(光标!= NULL)moreResultsUrl = cursor.get(moreResultsUrl)的toString()。
             }
         }

    }赶上(JSONException E){
        e.printStackTrace();
    }
}
 

}

How can I parse this URL in JSON format?

https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=&rsz=8"

public class MainActivity extends ListActivity {
    private Context context;
    private static String url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=pixelloid&rsz=8";

    private static final String TAG_VTYPE = "responseData";
    private static final String TAG_VCOLOR = "results";
    private static final String TAG_FUEL = "GsearchResultClass";
    private static final String TAG_TREAD = "url";
    private static final String TAG_OPERATOR = "url";
    private static final String TAG_NAME = "name";
    private static final String TAG_POINTS = "experiencePoints";

    ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();

    ListView lv ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new ProgressTask(MainActivity.this).execute();
    }

    private class ProgressTask extends AsyncTask<String, Void, Boolean> {
        private ProgressDialog dialog;

        private ListActivity activity;

        // private List<Message> messages;
        public ProgressTask(ListActivity activity) {
            this.activity = activity;
            context = activity;
            dialog = new ProgressDialog(context);
        }

        /** progress dialog to show user that the backup is processing. */

        /** application context. */
        private Context context;

        protected void onPreExecute() {
            this.dialog.setMessage("Progress start");
            this.dialog.show();
        }

        @Override
        protected void onPostExecute(final Boolean success) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
            ListAdapter adapter = new SimpleAdapter(context, jsonlist,
                    R.layout.list_item, new String[] { TAG_VTYPE, TAG_VCOLOR,
                            TAG_FUEL, TAG_TREAD }, new int[] {
                            R.id.vehicleType, R.id.vehicleColor, R.id.fuel,
                            R.id.treadType });

            setListAdapter(adapter);

            // selecting single ListView item
             lv = getListView();






        }

        protected Boolean doInBackground(final String... args) {

            JSONParser jParser = new JSONParser();

            // getting JSON string from URL
            JSONArray json = jParser.getJSONFromUrl(url);

            if (json == null) {
                   // notify user
                Log.e("==>", "Failed to download file");

            } 
             else {
            for (int i = 0; i < json.length(); i++) {

                try {
                    JSONObject c = json.getJSONObject(i);
                    String vtype = c.getString(TAG_VTYPE);

                    String vcolor = c.getString(TAG_VCOLOR);
                    String vfuel = c.getString(TAG_FUEL);
                    String vtread = c.getString(TAG_TREAD);

                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_VTYPE, vtype);
                    map.put(TAG_VCOLOR, vcolor);
                    map.put(TAG_FUEL, vfuel);
                    map.put(TAG_TREAD, vtread);
                    jsonlist.add(map);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Log.e("==>", "iyogbr;g;ui");

                }
            }
             }

            return null;
        }

    }

}

I'm parsing this data:

{"responseData":{"results":[{"content":"<b>Pixelloid<\/b> Studios Visual Effects, 3D Animation Production and Training Institute.","GsearchResultClass":"GwebSearch","titleNoFormatting":"Pixelloid - Visual Effects, 3D Animation - Production and Training","title":"<b>Pixelloid<\/b> - Visual Effects, 3D Animation - Production and Training","cacheUrl":"http:\/\/www.google.com\/search?q=cache:Uq3_USWdbwIJ:www.pixelloid.com","unescapedUrl":"http:\/\/www.pixelloid.com\/","url":"http:\/\/www.pixelloid.com\/","visibleUrl":"www.pixelloid.com"},{"content":"Pixelloid Studios Visual Effects, 3D   Animation Production and Training <b>...<\/b>","GsearchResultClass":"GwebSearch","titleNoFormatting":"Production and Training Institute - Pixelloid","title":"Production and Training Institute - <b>Pixelloid<\/b>","cacheUrl":"http:\/\/www.google.com\/search?q=cache:Pce2UIgSVDoJ:www.pixelloid.com","unescapedUrl":"http:\/\/www.pixelloid.com\/training\/","url":"http:\/\/www.pixelloid.com\/training\/","visibleUrl":"www.pixelloid.com"},{"content":"<b>Pixelloid<\/b> is an award winning 3D Animation &amp; Visual Effects Production and   Training Studio based in Hyderabad with branches in Bangalore, Visakhapatnam   <b>...<\/b>","GsearchResultClass":"GwebSearch","titleNoFormatting":"Pixelloid Studios - YouTube","title":"<b>Pixelloid<\/b> Studios - YouTube","cacheUrl":"http:\/\/www.google.com\/search?q=cache:wrfWBrpn45AJ:www.youtube.com","unescapedUrl":"http:\/\/www.youtube.com\/user\/pixelloid","url":"http:\/\/www.youtube.com\/user\/pixelloid","visibleUrl":"www.youtube.com"},{"content":"<b>Pixelloid<\/b>, Hyderabad, India. 5896 likes · 60 talking about this · 408 were here. We   help people tell their stories effectively. We do animation, visual effects for <b>...<\/b>","GsearchResultClass":"GwebSearch","titleNoFormatting":"Pixelloid - Hyderabad, India - Media\/News\/Publishing | Facebook","title":"<b>Pixelloid<\/b> - Hyderabad, India - Media\/News\/Publishing | Facebook","cacheUrl":"http:\/\/www.google.com\/search?q=cache:_m-PNJ9c3sgJ:www.facebook.com","unescapedUrl":"https:\/\/www.facebook.com\/Pixelloid","url":"https:\/\/www.facebook.com\/Pixelloid","visibleUrl":"www.facebook.com"},{"content":"Welcome to the company profile of <b>Pixelloid<\/b> Studios Private Limited on LinkedIn.   <b>Pixelloid<\/b> is an award winning Content Creation Company with an active <b>...<\/b>","GsearchResultClass":"GwebSearch","titleNoFormatting":"Pixelloid Studios Private Limited | LinkedIn","title":"<b>Pixelloid<\/b> Studios Private Limited | LinkedIn","cacheUrl":"http:\/\/www.google.com\/search?q=cache:vnyg3sUrGfgJ:www.linkedin.com","unescapedUrl":"http:\/\/www.linkedin.com\/company\/pixelloid-studios-private-limited","url":"http:\/\/www.linkedin.com\/company\/pixelloid-studios-private-limited","visibleUrl":"www.linkedin.com"},{"content":"The latest from <b>Pixelloid<\/b> (@<b>Pixelloid<\/b>). <b>Pixelloid<\/b> is an award winning 3D   Animation &amp; Visual Effects studio with a dedicated commercial training division.","GsearchResultClass":"GwebSearch","titleNoFormatting":"Pixelloid (Pixelloid) on Twitter","title":"<b>Pixelloid<\/b> (<b>Pixelloid<\/b>) on Twitter","cacheUrl":"http:\/\/www.google.com\/search?q=cache:s4gJgJcovCsJ:twitter.com","unescapedUrl":"https:\/\/twitter.com\/Pixelloid","url":"https:\/\/twitter.com\/Pixelloid","visibleUrl":"twitter.com"},{"content":"Feb 10, 2013 <b>...<\/b> <b>Pixelloid<\/b>, Hyderabad is the only training institute in India that has an active   production environment. <b>Pixelloid&#39;s<\/b> courses are tailored to the <b>...<\/b>","GsearchResultClass":"GwebSearch","titleNoFormatting":"Pixelloid - Admission, Courses, Fee, Address | Pixelloid | Hyderabad ...","title":"<b>Pixelloid<\/b> - Admission, Courses, Fee, Address | <b>Pixelloid<\/b> | Hyderabad <b>...<\/b>","cacheUrl":"http:\/\/www.google.com\/search?q=cache:3OWFak05f30J:www.minglebox.com","unescapedUrl":"http:\/\/www.minglebox.com\/college\/pixelloid-Hyderabad","url":"http:\/\/www.mi

解决方案

You are misunderstanding the way json serializes objects. Basically (very basically) {...} are objects, [...,...] are arrays and key:value are object properties, so something like:

{
  "responseData": {
     "results": [],
     "cursor": {
         "moreResultsUrl": "http://www.url.com",
         "searchResultTime": "0,00"
     }
  },
  "responseDetails": null,
  "responseStatus": 200
}

Means that you have a first object that has 3 properties: responseData(object), responseDetails(null object) and responseStatus(int). And responseData is an object with 2 properties: results (array) and cursor(object). And cursor's properties are moreResultsUrl(string), and searchResultTime(string).

and now, for parsing the json you can try something like this:

  • Take in mind that your code provided was too cloned-from-tutorial. You will have to edit things a little bit more in order to maintain some grade of parameter naming (your variables talk about cars and your data about web searches...). And you may rename some data from ListAdapter adapter = new Simple... method in order to fit your needs.

** In order to use these JSON methods, you need to include the simple json library.

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;

public class MainActivity extends ListActivity { 

private Context context; 
private static String url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=pixelloid&rsz=8";

private static final String TAG_content = "content";
private static final String TAG_GsearchResultClass = "GsearchResultClass";
private static final String TAG_titleNoFormatting = "titleNoFormatting";
private static final String TAG_title = "title";
private static final String TAG_cacheUrl = "cacheUrl";
private static final String TAG_unescapedUrl = "unescapedUrl";
private static final String TAG_url = "url";
private static final String TAG_visibleUrl = "visibleUrl";



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new ProgressTask(MainActivity.this).execute();
}

private class ProgressTask extends AsyncTask<String, Void, Boolean> {
    private ProgressDialog dialog;

    private ListActivity activity;

    // private List<Message> messages;
    public ProgressTask(ListActivity activity) {
        this.activity = activity;
        context = activity;
        dialog = new ProgressDialog(context);
    }

    /** progress dialog to show user that the backup is processing. */

    /** application context. */
    private Context context;

    protected void onPreExecute() {
        this.dialog.setMessage("Progress start");
        this.dialog.show();
    }

    @Override
    protected void onPostExecute(final Boolean success) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
        ListAdapter adapter = new SimpleAdapter( context, contents, R.layout.list_item, 
                                    new String[] { TAG_title, TAG_content, TAG_url }, 
                                    new int[] { R.id.vehicleType, R.id.vehicleColor, R.id.fuel, R.id.treadType });

        setListAdapter(adapter);

        // selecting single ListView item
        //lv = getListView();
    }

    protected Boolean doInBackground(final String... args) {
            loadJson();
            return null;
    }
}

public static JSONObject parse() {
    JSONParser parser = new JSONParser();
    Object obj;
    JSONObject json = new JSONObject();
    try {
        obj = parser.getJSONFromUrl(url);
        json=(JSONObject)obj;
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return json;
}

private String moreResultsUrl = "";
private ArrayList<HashMap<String, String>> contents;

public void loadJson() {

    try {
        JSONObject obj = parse();
        if(obj!=null) {
             JSONObject responseData;
             responseData = (JSONObject ) obj.get("responseData");

             if(responseData !=null) {
                JSONArray results =  (JSONArray) obj.get("results"); 
                contents = new ArrayList<HashMap<String, String>>();

                HashMap<String, String> map;
                JSONObject cro;
                for(int i=0;i<results.length();i++) {
                    map = new HashMap<String, String>();
                    cro = (JSONObject) results.get(i);

                    map.put(TAG_content, cro.get(TAG_content).toString() );
                    map.put(TAG_GsearchResultClass, cro.get(TAG_GsearchResultClass).toString() );
                    map.put(TAG_titleNoFormatting, cro.get(TAG_titleNoFormatting).toString() );
                    map.put(TAG_titleNoFormatting, cro.get(TAG_titleNoFormatting).toString() );
                    map.put(TAG_title, cro.get(TAG_title).toString() );
                    map.put(TAG_cacheUrl, cro.get(TAG_cacheUrl).toString() );
                    map.put(TAG_unescapedUrl, cro.get(TAG_unescapedUrl).toString() );
                    map.put(TAG_url, cro.get(TAG_url).toString() );
                    map.put(TAG_visibleUrl, cro.get(TAG_visibleUrl).toString() );

                    contents.add(map);
                }

                JSONObject cursor =  (JSONObject ) responseData.get("cursor");
                if(cursor!=null)  moreResultsUrl = cursor.get("moreResultsUrl").toString();
             }
         }

    } catch (JSONException e) {
        e.printStackTrace();
    }
}

}

这篇关于谷歌阿比解析的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆