1

I am getting a JSON Array.

[{"0":"500","post_id":"500","1":"Ps-2","post_title":"Ps-2","2":"Ps-2 in new condition. used only for 15 to 20 times.\n3 dvd's will also be given freely.\nthis all for a nominal price","post_des":"Ps-2 in new condition. used only for 15 to 20 times.\n3 dvd's will also be given freely.\nthis all for a nominal price","3":"2013-02-16 04:02:24","post_date":"2013-02-16 04:02:24","4":"2013-02-16 04:02:24","edit_date":"2013-02-16 04:02:24","5":"1","post_status":"1","6":"5","user_id":"5","7":"0","featuered":"0","8":"11","sub_cat_id":"11","9":"1","cat_id":"1","10":"7500","post_price":"7500","11":".\/uploads\/lmf-classifieds-pk\/folder1\/2013-02-16\/500\/1361014226-ps-2-lmf-com-pk.jpg","post_img":".\/uploads\/lmf-classifieds-pk\/folder1\/2013-02-16\/500\/1361014226-ps-2-lmf-com-pk.jpg","12":"1","post_country_id":"1","13":"7","post_state_id":"7","14":"92","post_city_id":"92","15":"olx","post_ref":"olx","16":"03335525488","number":"03335525488","17":"7","state_id":"7","18":"Punjab","state_title":"Punjab","19":"Punjab","state_des":"Punjab","20":"1","state_status":"1","21":"1","country_id":"1","22":"populer","state_type":"populer","23":"92","city_id":"92","24":"Rawalpindi","city_title":"Rawalpindi","25":"Rawalpindi","city_des":"Rawalpindi","26":"1","city_status":"1","27":"7","28":"populer","city_type":"populer","29":"populer","is_important":"populer","30":"16-February-2013","p_date":"16-February-2013","image1":"..\/uploads\/lmf-classifieds-pk\/folder1\/2013-02-16\/500\/1361014226-ps-2-lmf-com-pk.jpg","image2":"..\/uploads\/lmf-classifieds-pk\/folder1\/2013-02-16\/500\/1361014225-ps-2-lmf-com-pk.jpg"}]

I then convert that array into a JSON object. How can i check if the "number" index exists in this array or not?

Here is my Complete Code :

package com.example.lmf;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class adsDetail extends Activity {

    String id = "";
    String title = null;
    String desc = null;
    String num = "";
    ArrayList<String> files = new ArrayList<String>();
    String arr[]  = {};
    Context context = this;
    final static String URL = "http://www.lmf.com.pk/admin/json.php?";
    final getInternetData obj = new getInternetData();
    public ImageLoader imageLoader = null;
    public FileCache clearCache = null;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ads_detail);
        id = getIntent().getExtras().getInt("id")!= 0 ? "id="+getIntent().getExtras().getInt("id") : "";
        Button share = (Button) findViewById(R.id.share);
        share.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //create the send intent
                Intent shareIntent = 
                 new Intent(android.content.Intent.ACTION_SEND);

                //set the type
                shareIntent.setType("text/plain");

                //add a subject
                shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, 
                 title);

                //build the body of the message to be shared
                String shareMessage = "http://lmf.com.pk?single_ad.php?title="+title+"&"+id;

                //add the message
                shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, 
                 shareMessage);

                //start the chooser for sharing
                startActivity(Intent.createChooser(shareIntent, 
                 title));

            }
        });

        final ImageView call = (ImageView) findViewById(R.id.imageView2);
        call.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                 String uri = "tel:" + num.trim() ;
                 Intent intent = new Intent(Intent.ACTION_DIAL);
                 intent.setData(Uri.parse(uri));
                 startActivity(intent);

            }
        });
        //obj.showProgress(context);
        new getBackgroundData().execute();

    }

////////////Async Class

private class getBackgroundData extends AsyncTask<Void, Integer, JSONArray>
{
@Override
protected JSONArray doInBackground(Void... params) 
{
try
{
JSONArray array = obj.getDATA(id,URL);
return array;

}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}


@Override
protected void onPostExecute(JSONArray array) 
{

try
{

JSONObject row = array.getJSONObject(0);

//// Post Title Creation start here /////////

TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(row.getString("post_title"));
title = row.getString("post_title");
//// Post Title Creating Ends here /////////

//// Getting Phone Number //////////////////
if(row.has("number"))
{
    num = row.getString("number");
}
else
{
    num = "123";
}

Toast.makeText(context, num, Toast.LENGTH_LONG).show();
//// Getting Number Ends HEre /////////////

////Post Price Creation start here /////////
TextView price = (TextView) findViewById(R.id.textView2);
if(row.getString("post_price")!="" && row.getString("post_price")!="FREE")
{
    price.setText(row.getString("post_price"));
}
else
{
    price.setText(row.getString("post_price"));
}
    price.setTextColor(Color.LTGRAY);
    price.setTextSize(16);
    price.setTypeface(null,Typeface.BOLD);

////Post Price Creating Ends here /////////

////Post user Creation start here /////////
TextView user = (TextView) findViewById(R.id.post_user);
user.setText("Anonymouse");
////Post user Creating Ends here /////////

////Post state Creation start here /////////
TextView state = (TextView) findViewById(R.id.textView3);
state.setText(row.getString("state_title"));
state.setTextSize(10);
state.setTypeface(null,Typeface.BOLD);
////Post state Creating Ends here /////////

////Post city Creation start here /////////
TextView city = (TextView) findViewById(R.id.textView4);
city.setText(row.getString("city_title"));
city.setTextSize(9);
city.setTypeface(null,Typeface.BOLD);
////Post city Creating Ends here /////////

////Post post date Creation start here /////////
TextView date = (TextView) findViewById(R.id.post_date);
date.setText("Posted On : "+row.getString("p_date"));
////Post post date Creating Ends here /////////

////Post Price Creation start here /////////
TextView post_des = (TextView) findViewById(R.id.post_detail);
post_des.setText(row.getString("post_des").replaceAll("\\<.*?>",""));
desc = row.getString("post_des");
////Post Price Creating Ends here /////////



//// Image Creating Starts from  here /////////
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
String[] parts = row.getString("post_img").split("/");
int last_index = parts.length - 1;
//String str = "";
for(int j=0; j<last_index; j++)
{
    //str += parts[j]+"/";
}

String path = "http://www.lmf.com.pk/"+row.getString("post_img");
getImageUrls(path);
Drawable image = drawable_from_url(path,"");
imageView.setBackgroundDrawable(image);
imageView.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent i = new Intent(context,com.example.lmf.ImageGallery.class);
        i.putExtra("imageUrlArray", files.toArray(new String[files.size()]));
        startActivity(i);
    }
});

//// Image Creating Ends here /////////

//obj.hideProgress();

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

}

}


}


/////////// Ends Here
@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    MenuItem item = menu.add ("Quit");
    MenuItem item2 = menu.add ("Clear Cache");
    item.setOnMenuItemClickListener (new OnMenuItemClickListener()
    {
        @Override
        public boolean onMenuItemClick (MenuItem item)
        {
            //clearArray();
            finish();
            return true;
        }
    });

    item2.setOnMenuItemClickListener (new OnMenuItemClickListener()
    {
        @Override
        public boolean onMenuItemClick (MenuItem item)
        {
            //clearCahe();
            clearCache.clear();
            return true;
        }
    });
            return true;
}

Drawable drawable_from_url(String url, String src_name) throws 
java.net.MalformedURLException, java.io.IOException 
{
return Drawable.createFromStream(((java.io.InputStream)
   new java.net.URL(url).getContent()), src_name);
}

public void getImageUrls(String path) 
{
    files.add(path);

} 



}
4

1 回答 1

3

我刚刚尝试过这个并且它有效。你在做类似的事情吗?

JSONArray array = null;
String num = "";
try {
    array = new JSONArray("[{\"0\":\"500\",\"post_id\":\"500\",\"1\":\"Ps-2\",\"post_title\":\"Ps-2\",\"2\":\"Ps-2 in new condition. used only for 15 to 20 times.\\n3 dvd's will also be given freely.\\nthis all for a nominal price\",\"post_des\":\"Ps-2 in new condition. used only for 15 to 20 times.\\n3 dvd's will also be given freely.\\nthis all for a nominal price\",\"3\":\"2013-02-16 04:02:24\",\"post_date\":\"2013-02-16 04:02:24\",\"4\":\"2013-02-16 04:02:24\",\"edit_date\":\"2013-02-16 04:02:24\",\"5\":\"1\",\"post_status\":\"1\",\"6\":\"5\",\"user_id\":\"5\",\"7\":\"0\",\"featuered\":\"0\",\"8\":\"11\",\"sub_cat_id\":\"11\",\"9\":\"1\",\"cat_id\":\"1\",\"10\":\"7500\",\"post_price\":\"7500\",\"11\":\".\\/uploads\\/lmf-classifieds-pk\\/folder1\\/2013-02-16\\/500\\/1361014226-ps-2-lmf-com-pk.jpg\",\"post_img\":\".\\/uploads\\/lmf-classifieds-pk\\/folder1\\/2013-02-16\\/500\\/1361014226-ps-2-lmf-com-pk.jpg\",\"12\":\"1\",\"post_country_id\":\"1\",\"13\":\"7\",\"post_state_id\":\"7\",\"14\":\"92\",\"post_city_id\":\"92\",\"15\":\"olx\",\"post_ref\":\"olx\",\"16\":\"03335525488\",\"number\":\"03335525488\",\"17\":\"7\",\"state_id\":\"7\",\"18\":\"Punjab\",\"state_title\":\"Punjab\",\"19\":\"Punjab\",\"state_des\":\"Punjab\",\"20\":\"1\",\"state_status\":\"1\",\"21\":\"1\",\"country_id\":\"1\",\"22\":\"populer\",\"state_type\":\"populer\",\"23\":\"92\",\"city_id\":\"92\",\"24\":\"Rawalpindi\",\"city_title\":\"Rawalpindi\",\"25\":\"Rawalpindi\",\"city_des\":\"Rawalpindi\",\"26\":\"1\",\"city_status\":\"1\",\"27\":\"7\",\"28\":\"populer\",\"city_type\":\"populer\",\"29\":\"populer\",\"is_important\":\"populer\",\"30\":\"16-February-2013\",\"p_date\":\"16-February-2013\",\"image1\":\"..\\/uploads\\/lmf-classifieds-pk\\/folder1\\/2013-02-16\\/500\\/1361014226-ps-2-lmf-com-pk.jpg\",\"image2\":\"..\\/uploads\\/lmf-classifieds-pk\\/folder1\\/2013-02-16\\/500\\/1361014225-ps-2-lmf-com-pk.jpg\"}]\n");
    JSONObject row = array.getJSONObject(0);
    if(row.has("number")) {
        num = row.getString("number");
    }

} catch (JSONException e) {

}
Toast.makeText(this, num, Toast.LENGTH_LONG);
于 2013-05-03T22:53:11.487 回答