0

我有这个 JSON 数组:

[{"id":"101","title":"Oferta 1"},{"id":"102","title":"Oferta 2"},{"id":"103","title":"Oferta del Mes"},{"id":"104","title":"Promoci\u00f3n Facebook"}]

我需要解析这个 JSON,但是当我解析它时,我只接收

{"id":"101","title":"Oferta 1"}

这是我的代码:

try {
    JSONObject json = JSONfunctions.getJSONfromURL("URLOFJSON");
    Log.i("log_tag", json.toString()); 
    String jsonvalues =  json.getString("id");

    Log.i("log_tag", jsonvalues);  
}
catch (Exception ex)
{
    Log.e("log_tag", "Error getJSONfromURL "+ex.toString());           
}

我该如何解决这个问题?

谢谢大家。

4

3 回答 3

1

问题是你正在获取一个 jsonObject,获取一个 json 数组并从中检索 json 对象虚拟代码

JSonArray ja;
int resultCount = ja.length();
for (int i = 0; i < resultCount; i++)
{
    JSONObject resultObject = ja.getJSONObject(i);
    String id = resultObject.getString("id");

}
于 2012-09-20T16:54:25.997 回答
0

要从 json 数据中获取数据,请使用以下代码行。

String my_info = investors.getProfileAbout(user_id).toString();
            LinearLayout about =(LinearLayout) findViewById(R.id.prof_about);
            about.setVisibility(View.VISIBLE);
            try {
                JSONArray array = new JSONArray(my_info);
                for (int i = 0; i < array.length(); i++) {
                    JSONObject row = array.getJSONObject(i);
                    //id = row.getInt('id');
                    String type1 = row.getString("type");
                    String person_marks = row.getString("follwed");
                    String total_posts = row.getString("totalpost");
                    String componsated = row.getString("ipost");
                    String mods = row.getString("mods");
                    String  doj = row.getString("bday");

                    String avatar = row.getString("avatar");


                   TextView type = (TextView) findViewById(R.id.mebership);
                   type.setText(type1);

                   TextView marks = (TextView) findViewById(R.id.marks);
                   marks.setText(person_marks);

                   TextView total_pos = (TextView) findViewById(R.id.total_posts);
                   total_pos.setText(total_posts);

                   TextView componsated_posts = (TextView) findViewById(R.id.componsated_posts);
                   componsated_posts.setText(componsated);

                   TextView moderating = (TextView) findViewById(R.id.moderating);
                   moderating.setText(mods);

                   TextView user_name = (TextView) findViewById(R.id.profile_username);
                   user_name.setText(extras.getString("user_name"));

                   ImageView userProfilePic = (ImageView) findViewById(R.id.user_profile_img);
                   userProfilePic.setImageBitmap(mySession.downloadImage(avatar));

                   TextView dob = (TextView) findViewById(R.id.dob);
                    dob.setText(doj);
                }

                } catch (Exception e) {
                Log.e("Exception", "Exception when parsing response JSON."+e.getMessage());
                }

有关更多详细信息,请单击此处http://grabcodes.blogspot.com

于 2012-09-20T17:07:02.477 回答
0

我用这段代码解决了。

URL urlws = new URL(
                "URLOFFJSON");
        URLConnection tc = urlws.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                tc.getInputStream()));
        String line;

        while ((line = in.readLine()) != null) {
            JSONArray ja = new JSONArray(line);
            for (int i = 0; i < ja.length(); i++) {
                JSONObject jo = (JSONObject) ja.get(i);
                Log.i("log_tag", jo.toString());
            }
        }

这样我就可以获得网络服务 JSONArray

谢谢大家

于 2012-09-21T11:57:41.610 回答