如何解析json数组中的数组

时间:2012-10-08 15:42:55

标签: android

我正在解析json但是在我的jsonarray中我有另一个数组,这个数组值没有标签。(属性名称。)这里是我的代码..我可以解析其他值而不是图库数组。我如何解析图库onitemclick方法中的数组值?感谢...

Myjson链接:http://kilimmobilya.com.tr/mobileservices/default.aspx?i = yeniUrunler

我的json

{
    "veri": [
        {
            "id": "1436",
            "tarih": "08.10.2012",
            "baslik": "Ares Plazma TV \u00dcnitesi",
            "kImaj": "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Plazma-Unitesi/210x120/Ares-Plazma-VS1-00.jpg",
            "kisaAciklama": "",
            "icerik": "",
            "fiyat": "799",
            "Gallery": [
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Plazma-Unitesi/630x360/Ares-Plazma-VS1-00.jpg",
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Plazma-Unitesi/630x360/Ares-Plazma-VS1-01.jpg",
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Plazma-Unitesi/630x360/Ares-Plazma-VS1-02.jpg",
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Plazma-Unitesi/630x360/Ares-Plazma-VS2-00.jpg",
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Plazma-Unitesi/630x360/Ares-Plazma-VS2-01.jpg"
            ]
        },
        {
            "id": "1434",
            "tarih": "08.10.2012",
            "baslik": "Ares Yatak Odas\u0131",
            "kImaj": "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Yatak-Odasi/210x120/Ares-Yatak-Odasi-Takim-00.jpg",
            "kisaAciklama": "",
            "icerik": "",
            "fiyat": "5690",
            "Gallery": [
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Yatak-Odasi/630x360/Ares-Yatak-Odasi-Takim-00.jpg",
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Yatak-Odasi/630x360/Ares-Yatak-Odasi-Takim-00-1.jpg",
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Yatak-Odasi/630x360/Ares-Yatak-Odasi-Takim-01.jpg",
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Yatak-Odasi/630x360/Ares-Yatak-Odasi-Takim-02.jpg",
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Yatak-Odasi/630x360/Ares-Yatak-Odasi-Takim-03.jpg",
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Yatak-Odasi/630x360/Ares-Yatak-Odasi-Takim-04.jpg",
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Yatak-Odasi/630x360/Ares-Yatak-Odasi-Takim-05.jpg",
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Yatak-Odasi/630x360/Ares-Yatak-Odasi-Takim-06.jpg",
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Yatak-Odasi/630x360/Ares-Yatak-Odasi-Takim-07.jpg",
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Yatak-Odasi/630x360/Ares-Yatak-Odasi-Takim-08.jpg",
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Yatak-Odasi/630x360/Ares-Yatak-Odasi-Takim-09.jpg",
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Yatak-Odasi/630x360/Ares-Yatak-Odasi-Takim-10.jpg",
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Yatak-Odasi/630x360/Ares-Yatak-Odasi-Takim-11.jpg",
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Yatak-Odasi/630x360/Ares-Yatak-Odasi-Takim-12.jpg",
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Yatak-Odasi/630x360/Ares-Yatak-Odasi-Takim-13.jpg",
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Yatak-Odasi/630x360/Ares-Yatak-Odasi-Takim-14.jpg",
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Yatak-Odasi/630x360/Ares-Yatak-Odasi-Takim-15.jpg",
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Yatak-Odasi/630x360/Ares-Yatak-Odasi-Takim-16.jpg",
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Yatak-Odasi/630x360/Ares-Yatak-Odasi-Takim-17.jpg",
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Yatak-Odasi/630x360/Ares-Yatak-Odasi-Takim-18.jpg",
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Yatak-Odasi/630x360/Ares-Yatak-Odasi-Takim-19.jpg",
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Yatak-Odasi/630x360/Ares-Yatak-Odasi-Takim-20.jpg",
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Yatak-Odasi/630x360/Ares-Yatak-Odasi-Takim-21.jpg",
                "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Yatak-Odasi/630x360/Ares-Yatak-Odasi-Takim-22.jpg"
            ]
        }
package com.eticaret.hakan;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

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

import com.eticaret.hakan.R;

import android.R.string;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Yeniurunler extends Activity implements OnItemClickListener,OnClickListener {
    int gelencatid;
    Button altkampanya,altkatalog,altbayi,altiletisim;
    private static final String rssFeed = "http://kilimmobilya.com.tr/mobileservices/default.aspx?i=yeniUrunler";
    private static final String link="http://salih.arti-sanat.com/e-ticaret/s3.jpg";

    private static final String TAG_PRODUCTS = "veri";
    private static final String TAG_PID = "id";
    private static final String TAG_PNAME = "baslik";
    private static final String TAG_PPRICE = "tarih";
    private static final String TAG_PDESCRIPTION = "kisaAciklama";
    private static final String TAG_CREATEDAT = "tarih";

    private static final String TAG_LINK = "kImaj";
    private static final String TAG_GALLERY="Gallery";

    List<Item> arrayOfList;
    ListView listView;
    KampanyaRowAdapter objAdapter;
    JSONArray jsonArray;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
          requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.yeniurunler);
        altkampanya=(Button) findViewById(R.id.altkampanyalar);
        altkatalog=(Button) findViewById(R.id.altkataloglar);
        altbayi=(Button) findViewById(R.id.altbayilerimiz);
        altiletisim=(Button) findViewById(R.id.altiletisim);
        altkampanya.setOnClickListener(this);
        altkatalog.setOnClickListener(this);
        altbayi.setOnClickListener(this);
        altiletisim.setOnClickListener(this);
        listView = (ListView) findViewById(R.id.listview);
        listView.setDivider(new ColorDrawable(0x90000000)); 
        listView.setDividerHeight(1);
        listView.setOnItemClickListener(this);

        arrayOfList = new ArrayList<Item>();

        if (Utils.isNetworkAvailable(Yeniurunler.this)) {
            new MyTask().execute(rssFeed);
        } else {
            showToast("Ağ bağlantısı yok!!!");
        }

    }

    // My AsyncTask start...

    class MyTask extends AsyncTask<String, Void, String> {

        ProgressDialog pDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            pDialog = new ProgressDialog(Yeniurunler.this);
            pDialog.setMessage("Yükleniyor...");
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... params) {
            return Utils.getJSONString(params[0]);
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            if (null != pDialog && pDialog.isShowing()) {
                pDialog.dismiss();
            }

            if (null == result || result.length() == 0) {
                showToast("Ürün Bulunamadı");
                Yeniurunler.this.finish();
            } else {

                try {
                    JSONObject mainJson = new JSONObject(result);
                    jsonArray = mainJson.getJSONArray(TAG_PRODUCTS);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject objJson = jsonArray.getJSONObject(i);

                        Item objItem = new Item();


                        objItem.setName(objJson.getString(TAG_PNAME));
                        objItem.setCity(objJson.getString(TAG_PDESCRIPTION));
                        objItem.setLink(objJson.getString(TAG_LINK));

                        //objItem.setLink(link);



                        arrayOfList.add(objItem);


                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                // check data...

                /*
                 * for (int i = 0; i < arrayOfList.size(); i++) { Item item =
                 * arrayOfList.get(i); System.out.println(item.getId());
                 * 
                 * System.out.println(item.getId());
                 * System.out.println(item.getName());
                 * System.out.println(item.getCity());
                 * System.out.println(item.getGender());
                 * System.out.println(item.getAge());
                 * System.out.println(item.getBirthdate()); }
                 */

                Collections.sort(arrayOfList, new Comparator<Item>() {

                    public int compare(Item lhs, Item rhs) {
                        return (lhs.getAge() - rhs.getAge());
                    }
                });
                setAdapterToListview();

            }

        }
    }

    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {


                Intent a = new Intent(getApplicationContext(), YeniUrunDetay.class);
                // starting new activity and expecting some response back

                startActivity(a);





        // sending pid to next activity


        }


    public void setAdapterToListview() {
        objAdapter = new KampanyaRowAdapter(Yeniurunler.this, R.layout.kampanyarow,
                arrayOfList);
        listView.setAdapter(objAdapter);
    }

    public void showToast(String msg) {
        Toast.makeText(Yeniurunler.this, msg, Toast.LENGTH_LONG).show();
    }


    public void onClick(View v) {
        // TODO Auto-generated method stub
switch (v.getId()) {

        case R.id.altkampanyalar:
            Intent goAltkampanya= new Intent(this, Kampanyalar.class);
            startActivity(goAltkampanya);

            break;
        case R.id.altkataloglar:
            Intent goAltkatalog = new Intent(this, Kataloglar.class);
            startActivity(goAltkatalog);

            break;
        case R.id.altbayilerimiz:
            Intent goBAyi= new Intent(this, Bayiler.class);
            startActivity(goBAyi);

            break;
        case R.id.altiletisim:
            Intent goAltiletisim= new Intent(this, Iletisim.class);
            startActivity(goAltiletisim);

            break;
        }

    }

}

2 个答案:

答案 0 :(得分:0)

您可以通过相应地定义对象来使用Gson。 对于Arrays,自然不会有字段名称,因此可以将它们解析为List。

这里有一个使用此网址响应的示例 http://kilimmobilya.com.tr/mobileservices/default.aspx?i=yeniUrunler

响应数组中的JsonObject是;

{
id: "1436",
tarih: "08.10.2012",
baslik: "Ares Plazma TV Ünitesi",
kImaj: "http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Plazma-Unitesi/210x120/Ares-Plazma-VS1-00.jpg",
kisaAciklama: "",
icerik: "",
fiyat: "799",
Gallery: [
"http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Plazma-Unitesi/630x360/Ares-Plazma-VS1-00.jpg",
"http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Plazma-Unitesi/630x360/Ares-Plazma-VS1-01.jpg",
"http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Plazma-Unitesi/630x360/Ares-Plazma-VS1-02.jpg",
"http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Plazma-Unitesi/630x360/Ares-Plazma-VS2-00.jpg",
"http://www.kilimmobilya.com.tr/urunler/PANEL-GRUP/Ares-Plazma-Unitesi/630x360/Ares-Plazma-VS2-01.jpg"
]
},    

创建一个Java对象;

public class Urun implements Serializable {

    private static final long serialVersionUID = 1L;

    @Expose public String id;
    @Expose public String tarih;
    @Expose public String baslik;
    @Expose public String kImaj;
    @Expose public String kisaAciklama;
    @Expose public String icerik;
    @Expose public String fiyat;
    @Expose public List<String> Gallery;// notice this is a List of String

} 

然后您可以轻松地将JsonObject解析为已定义的类。使用Gson的例子

Urun urun = (new Gson()).fromJson(jsonObj, Urun.class);

希望这会有所帮助...

答案 1 :(得分:0)

这是你的POJO

public class Veri {
    private long id;
    private String date;
    private String baslik;
    private String kImagj;
    private String kisaAciklama;
    private String icerik;
    private String fiyat;
    private List<String> gallery;

    // Constructors, getters and setters…
}

以下是解析的代码

private List<Veri> parseJson(String json) {
    List<Veri> veriList = new ArrayList<Veri>();

    List<String> galleryImages = new ArrayList<String>();
    try {
        JSONObject jsonObject = new JSONObject(json);
        JSONArray veriArray = jsonObject.getJSONArray("veri");
        int nVeri = veriArray.length();
        for (int i = 0; i < nVeri; i++) {
            JSONObject veriObject = veriArray.getJSONObject(i);
            long id = Long.valueOf(veriObject.getString("id"));
            String date = veriObject.getString("tarih");
            String baslik = veriObject.getString("baslik");
            String kImaj = veriObject.getString("kImaj");
            String kisaAciklama = veriObject.getString("kisaAciklama");
            String icerik = veriObject.getString("icerik");
            String fiyat = veriObject.getString("fiyat");
            JSONArray galleryImageArray = veriObject.getJSONArray("Gallery");

            int nImages = galleryImageArray.length();
            for(int j = 0; j < nImages; i++) {
                galleryImages.add(galleryImageArray.getString(j));
            }

            Veri veri = new Veri();
            veri.setId(id);
            veri.setDate(date);
            veri.setBaslik(baslik);
            veri.setKImagj(kImaj);
            veri.setKisaAciklama(kisaAciklama);
            veri.setFiyat(fiyat);
            veri.setGallery(galleryImages);

            veriList.add(veri);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return veriList;
}

这可能会有所帮助!