ANDROID创建一个JSON提要一个String [] [英] Android create a String[] from a Json Feed

查看:139
本文介绍了ANDROID创建一个JSON提要一个String []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个懒惰的名单,在目前的图像列表加载URL是在String []硬codeD,我想通过一个JSON饲料加载它们。所以我的问题是我怎么可以创建一个JSONObject的字符串[]?

继承人什么香港专业教育学院走到这一步

  {尝试
        post.setEntity(新UrlEn codedFormEntity(对));
        HTT presponse响应= client.execute(岗位);
        的BufferedReader读者=新的BufferedReader(新的InputStreamReader(response.getEntity()的getContent(),UTF-8)。);
        JSON字符串= reader.readLine();
        fulldata =将String.valueOf(JSON);
        Log.v(对myApp,newsdata+ fulldata);        新闻列表=新的ArrayList<串GT;();
        newsList2 =新的ArrayList<串GT;();
        newsList3 =新的ArrayList<串GT;();        JSONObject的OBJ =新的JSONObject(JSON);
        JSONObject的objData = obj.getJSONObject(数据);
        JSONArray jArray = objData.getJSONArray(新闻);
           对于(INT T = 0; T< newsAmount;吨++){
               JSONObject的newsTitleDict = jArray.getJSONObject(T);//这是我想要的图像加载到一个String []
JSONArray ImageArray = objData.getJSONArray(新闻);             newsList3.add(newsTitleDict.getString(标题));           }           对于(INT T = 0; T< 1;吨++){
               JSONObject的newsTitleDict = jArray.getJSONObject(T);             newsList.add(newsTitleDict.getString(标题));
             newsList2.add(newsTitleDict.getString(标题));           }    }赶上(ClientProtocolException E){
        // TODO自动生成catch块
        e.printStackTrace();
    }赶上(IOException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
    }赶上(JSONException E){
        // TODO自动生成catch块
        e.printStackTrace();
    }
    arrayAdapter =新ArrayAdapter<串GT;(这一点,R.layout.single_item,新闻列表);
    arrayAdapter2 =新ArrayAdapter<串GT;(这一点,R.layout.single_item,newsList2);
    // arrayAdapter3 =新ArrayAdapter(这一点,R.layout.complex_item,newsList3);     的String [] = mStrings {
             http://a3.twimg.com/profile_images/670625317/aam-logo-v3-twitter.png
             http://a3.twimg.com/profile_images/740897825/AndroidCast-350_normal.png
             http://a3.twimg.com/profile_images/121630227/Droid_normal.jpg
             http://a1.twimg.com/profile_images/957149154/twitterhalf_normal.jpg
             http://a1.twimg.com/profile_images/97470808/icon_normal.png
     };     arrayAdapter3 =新LazyAdapter(这一点,mStrings);
        ListView控件列表= getListView();
           list.setTextFilterEnabled(真);           LayoutInflater吹气=(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            查看标题= inflater.inflate(R.layout.homeheader,列表,FALSE);
            查看HEADER2 = inflater.inflate(R.layout.homeheader2,列表,FALSE);
            查看header3 = inflater.inflate(R.layout.homeheader3,列表,FALSE);        适配器=新MergeAdapter();
        adapter.addView(头);
        adapter.addAdapter(arrayAdapter);
        adapter.addView(HEADER2);
        adapter.addAdapter(arrayAdapter2);
        adapter.addView(header3);
        adapter.addAdapter(arrayAdapter3);
        setListAdapter(适配器);    }


解决方案

这是我怎么添加值使用来自一个JSONObject一个ArrayList的String []。我的例子使用了一个JSONArray,但你可以随时更改以适应您的code。

 的ArrayList<串GT; arrayName中=新的ArrayList<串GT;();
ArrayList的<串GT; arrayPicture =新的ArrayList<串GT;();捆绑额外= getIntent()getExtras()。
apiResponse = extras.getString(API_RESPONSE);尝试{    JSONArray JAFriends =新JSONArray(apiResponse);    的for(int i = 0; I< JAFriends.length();我++){
        json_data = JAFriends.getJSONObject(ⅰ);
        如果(json_data.has(名字)){
            字符串getFriendName = json_data.getString(名称)与toUpperCase()。
            arrayName.add(getFriendName);
        }其他{
            字符串getFriendName = NULL;
            arrayName.add(getFriendName);
        }        如果(json_data.has(pic_square)){
            串getFriendPhoto = json_data.getString(pic_square);
            arrayPicture.add(getFriendPhoto);
        }其他{
            字符串getFriendPhoto = NULL;
            arrayPicture.add(getFriendPhoto);
        }
    }
}赶上(JSONException E){
    返回;
}
字符串名称=新的String [arrayName.size()];
字符串名称= arrayName.toArray(字符串名称);stringPicture =新的String [arrayPicture.size()];
stringPicture = arrayPicture.toArray(stringPicture);listofFriends =(ListView控件)findViewById(R.id.list);
适配器=新FriendsAdapter(这一点,字符串名称,stringPicture);listofFriends.setAdapter(适配器);

在他循环,使用的如果的语句,以确保没有错误蠕变,返回值添加到它们各自的的ArrayList和6号线在适配器之前,$ C $的C 设置,从的ArrayList中的值添加到字符串[]。
希望这有助于解决您的问题。

i have a lazy list that loads in a list of images at the moment the urls are hard coded in a String[] and i'm wanting to load them via a json feed. so my question is how can i create a string[] from a JsonObject?

heres what ive got so far

try {
        post.setEntity (new UrlEncodedFormEntity(pairs));
        HttpResponse response = client.execute(post);
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
        String json = reader.readLine();
        fulldata = String.valueOf(json);
        Log.v("myApp","newsdata" + fulldata);

        newsList = new ArrayList<String>();
        newsList2 = new ArrayList<String>();
        newsList3 = new ArrayList<String>();



        JSONObject obj = new JSONObject(json);    
        JSONObject objData = obj.getJSONObject("data");
        JSONArray jArray = objData.getJSONArray("news");


           for(int t = 0; t < newsAmount; t++){
               JSONObject newsTitleDict = jArray.getJSONObject(t);

//this is where i want to load the images into a String[]
JSONArray ImageArray = objData.getJSONArray("news");

             newsList3.add(newsTitleDict.getString("title"));

           }

           for(int t = 0; t < 1; t++){
               JSONObject newsTitleDict = jArray.getJSONObject(t);

             newsList.add(newsTitleDict.getString("title"));
             newsList2.add(newsTitleDict.getString("title"));

           }



    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }




    arrayAdapter = new ArrayAdapter<String>(this, R.layout.single_item, newsList);
    arrayAdapter2 = new ArrayAdapter<String>(this, R.layout.single_item, newsList2);
    //arrayAdapter3 = new ArrayAdapter(this, R.layout.complex_item, newsList3);



     String[] mStrings={
             "http://a3.twimg.com/profile_images/670625317/aam-logo-v3-twitter.png",
             "http://a3.twimg.com/profile_images/740897825/AndroidCast-350_normal.png",
             "http://a3.twimg.com/profile_images/121630227/Droid_normal.jpg",
             "http://a1.twimg.com/profile_images/957149154/twitterhalf_normal.jpg",
             "http://a1.twimg.com/profile_images/97470808/icon_normal.png",


     };



     arrayAdapter3 = new LazyAdapter(this, mStrings);


        ListView list = getListView();
           list.setTextFilterEnabled(true);

           LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE );
            View header = inflater.inflate( R.layout.homeheader, list, false);
            View header2 = inflater.inflate( R.layout.homeheader2, list, false);
            View header3 = inflater.inflate( R.layout.homeheader3, list, false);



        adapter = new MergeAdapter();
        adapter.addView(header);
        adapter.addAdapter(arrayAdapter);
        adapter.addView(header2);
        adapter.addAdapter(arrayAdapter2);
        adapter.addView(header3);
        adapter.addAdapter(arrayAdapter3);
        setListAdapter(adapter);

    }   

解决方案

This is how I add values to a String[] using an ArrayList from a JSONObject. My example uses a JSONArray, but you can always change that to fit your code.

ArrayList<String> arrayName = new ArrayList<String>();
ArrayList<String> arrayPicture = new ArrayList<String>();

Bundle extras = getIntent().getExtras();
apiResponse = extras.getString("API_RESPONSE");

try {

    JSONArray JAFriends = new JSONArray(apiResponse);

    for (int i = 0; i < JAFriends.length(); i++) {
        json_data = JAFriends.getJSONObject(i);


        if (json_data.has("name"))  {
            String getFriendName = json_data.getString("name").toUpperCase();
            arrayName.add(getFriendName);
        } else {
            String getFriendName = null;
            arrayName.add(getFriendName);
        }       

        if (json_data.has("pic_square"))    {
            String getFriendPhoto = json_data.getString("pic_square");
            arrayPicture.add(getFriendPhoto);
        } else {
            String getFriendPhoto = null;
            arrayPicture.add(getFriendPhoto);
        }
    }
} catch (JSONException e) {
    return;
}
stringName = new String[arrayName.size()];
stringName = arrayName.toArray(stringName);

stringPicture = new String[arrayPicture.size()];
stringPicture = arrayPicture.toArray(stringPicture);

listofFriends = (ListView)findViewById(R.id.list);
adapter = new FriendsAdapter(this, stringName, stringPicture);

listofFriends.setAdapter(adapter);

In he for loop, using if statements to ensure no errors creep in, the returned values are added to their respective ArrayLists and 6 lines of code before the adapter is set, the values from the ArrayLists are added to the String[]. Hope this helps solve your question.

这篇关于ANDROID创建一个JSON提要一个String []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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