raginggoat raginggoat - 1 month ago 7
Java Question

JSON Parsing Problems in Android

I'm trying to parse JSON in my Android app but I get this:

E/JSON Parser: Error parsing data org.json.JSONException: Value [{"image":"https:\/\/cdn.kyfb.com\/KYFB\/cache\/file\/70162997-E06B-E9B6-88514280CA8397CC_medium.jpg","description":"","link":"https:\/\/www.kyfb.com\/insurance\/insurance-products\/","name":"KFB Insurance","children":[]},{"image":"https:\/\/cdn.kyfb.com\/KYFB\/cache\/file\/07FF46E6-0A53-97A3-FD1CC6EA6374C5D2_medium.jpg","description":"","link":"https:\/\/www.farmbureaubank.com\/","name":"Farm Bureau Bank","children":[]},{"image":"https:\/\/cdn.kyfb.com\/KYFB\/cache\/file\/9CBB8FBA-C996-FB98-72983C1B70E4696C_medium.jpg","description":"","link":"","name":"Automotive","children":[{"image":"https:\/\/cdn.kyfb.com\/KYFB\/cache\/file\/E149B9CA-D232-DE5B-A3DA18BBFE905B6B_medium.jpg","description":"<p><strong>$500 off your next Chevrolet, GMC or Buick<\/strong><br \/>\r\n\tEligible Kentucky Farm Bureau members receive a $500 incentive toward the purchase or lease of most new <a href=\"http:\/\/www.chevrolet.com\" target=\"_blank\">Chevrolet&reg;<\/a>, <a href=\"http:\/\/www.buick.com\" target=\"_blank\">Buick&reg;<\/a>, and <a href=\"http:\/\/www.gm.com\" target=\"_blank\">GMC&reg;<\/a> cars, trucks, crossovers, vans and SUVs. The incentive can be stacked with most other available offers, including <a href=\"http:\/\/www.gmfleet.com\/2015-business-choice-offers.html\" target=\"_blank\">GM Business Choice<\/a>, Owner Loyalty and current in-market incentives (excludes military, employee, dealership employee and supplier pricing discounts).<\/p>\r\n\r\n<p>Go to <a href=\"http:\/\/www.fbverify.com\/gm\" target=\"_blank\">fbverify.com\/gm<\/a> to print your membership certificate. Must be a Kentucky Farm Bureau member for at least 30 days to be eligible. Must present certificate prior to completion of purchase.<\/p>","link":"","name":"GMC","children":[]},{"image":"https:\/\/cdn.kyfb.com\/KYFB\/cache\/file\/278ECCB8-F59C-EFAE-24ED00DBBB90413E_medium.jpg","description":"<p>Farm Bureau members receive a $300 per unit discount on Polaris Utility and Sport Vehicles, $200 per unit on All-Terrain Vehicles&nbsp;&nbsp;and&nbsp;&nbsp;$300 per unit on all GEM Electric Vehicles. All Youth and Polaris GENERAL model purchases are excluded. Members are eligible for the discounts 30 days after joining a participating Farm Bureau. Discounts are available for the purchase of specified Polaris off-road vehicles without exclusion in states that participate in the Polaris program.<\/p>\r\n\r\n<p>To take advantage of the Polaris program discounts through FB Member Advantage!, members log on to <a href=\"http:\/\/www.fbadvantage.com\/Deals\/Polaris\/#.U9EcudTD9fw\" target=\"_blank\">fbverify.com\/Polaris<\/a>&nbsp;where eligibility will be confirmed once a membership number and zip code is entered. A certificate will be made available online that must be printed and taken to the dealership of choice for presentation to the sales person. Certificates expire after 60 days.<\/p>","link":"","name":"Polaris","children":[]},{"image":"https:\/\/cdn.kyfb.com\/KYFB\/cache\/file\/E1D6BD36-C301-C63C-A8D177E824BE7BB2_medium.gif","description":"<p>Great Cars. Low Rates. Free Pickup.<\/p>\r\n\r\n<p>Save when you rent from Enterprise Rent-A-Car.&nbsp; With the largest fleet in North America and a wide variety of vehicles, Enterprise has you covered. Plus, we're always nearby with over 6,500 fully staffed neighborhood and airport locations.&nbsp; Pick Enterprise.&nbsp; We&rsquo;ll Pick You Up&reg;.&nbsp; &nbsp;<a href=\"http:\/\/www.enterprise.com\/en\/car-rental\/deals\/partners\/kentucky-farm-bureau.html\" target=\"_blank\">Book now<\/a>.<\/p>","link":"","name":"Enterprise Rent-A-Car","children":[]},{"image":"https:\/\/cdn.kyfb.com\/KYFB\/cache\/file\/E1F54677-0F7A-D8DB-9FAD9EB2851A7C0F_medium.gif","description":"<p><b>SAVE UP TO 25% OFF AVIS RENTALS<\/b><\/p>\r\n\r\n<p>Kentucky Farm Bureau members always receive up to 25 percent off Avis base rates* with Avis Worldwide Discount (AWD) number A298817. In addition, enjoy additional <a href=\"https:\/\/www.avis.com\/car-rental\/cont
I/System.out: JSON: null
I/System.out: BENEFITS: []


Here is my code:

public static class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";


public JSONParser()
{

}

public JSONObject getJSONFromUrl(String jsonUrl)
{
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(jsonUrl);
String responseBody = "DEFAULT_MSG_TEXT";
int resCode = 0;

try{

HttpResponse response = client.execute(get);

int responseCode = response.getStatusLine().getStatusCode();
resCode = responseCode;

switch(responseCode) {
case 200:
HttpEntity entity = response.getEntity();
if(entity != null) {
responseBody = EntityUtils.toString(entity);
}
break;
}
}
catch(Exception ex){
Log.e("Post Error",resCode + "\n Exception" + ex);
responseBody = "DEFAULT_MSG_TEXT";
}

json = responseBody;


try
{
jObj = new JSONObject(json);
}
catch (JSONException e)
{
Log.e("JSON Parser", "Error parsing data " + e.toString());
}


return jObj;
}
}


public class ParseJSON extends AsyncTask<Void,Void,ArrayList> {

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}

@Override
protected ArrayList doInBackground(Void... params) {
// TODO Auto-generated method stub

JSONParser jParser = new JSONParser();

// Get JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
System.out.println("JSON: " + json);

try {
JSONArray dataArray = json.getJSONArray("");

for(int i = 0; i <= dataArray.length(); i++) {
benefits.add(dataArray.get(i));
}

} catch(Exception e) {

}

System.out.println("BENEFITS: " + benefits);
return benefits;
}

@Override
protected void onPostExecute(ArrayList result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
prgLoading.setVisibility(View.GONE);
if(benefits != null){
listFeed.setVisibility(View.VISIBLE);
listFeed.setAdapter(la);
}else{
txtAlert.setVisibility(View.VISIBLE);
}
}
}


The URL is https://www.kyfb.com/KYFB/includes/themes/CodeBlue/display_objects/custom/remote/webservices/services.cfc?method=getMemberBenefits

Answer

The string you are trying to parse is a JSONArray, not a JSONObject. You should parse with new JSONArray(json).

Comments