Skip to content Skip to sidebar Skip to footer

Parsejson.java, Parsing Result If No Array Name?

This is my code: import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; /** * Created by Belal on 9/22/2015. */ public class ParseJSON { publi

Solution 1:

Unfortunately I don't follow your code. Base on what you have provide I don't see any JSON Array. As an extension to this response I made few modifications and access the data in the JSON object. Here are the additions.

Updates to MainActivity.java from here

HandleJSON obj = new HandleJSON("");
obj.readAndParseJSON(responseOutput.toString());
output.append(System.getProperty("line.separator") +"\nJSON Object... "+System.getProperty("line.separator") +"Make : "+ obj.getMake() +"\nModel : "+ obj.getModel());
output.append("\nSix Month Rate  : "+ obj.getSixMonthRate() +"\nTwelve Month Rate : "+ obj.getTwelveMonthRate() +"\nDate of First Registration : "+ obj.getDateofFirstRegistrationegistration());
output.append("\nYear of Manufacture : "+ obj.getYearOfManufacture() +"\nCylinder Capacity : "+ obj.getCylinderCapacity() +"\nCO2 Emmissions : "+ obj.getCo2Emissions());

HandleJSON.java

import org.json.JSONObject;
import android.annotation.SuppressLint;

publicclassHandleJSON {
    privateString make = "make";
    privateString model = "model";
    privateString sixMonthRate = "sixMonthRate";
    privateString twelveMonthRate = "twelveMonthRate";
    privateString dateofFirstRegistration = "dateofFirstRegistration";
    privateString yearOfManufacture = "yearOfManufacture";
    privateString cylinderCapacity = "cylinderCapacity";
    privateString co2Emissions= "co2Emissions";

    publicHandleJSON(String url) {this.urlString = url;}
    publicvoidsetURL(String url){ this.urlString = url;}
    publicStringgetMake() {return make;}
    publicStringgetModel() {return model;}
    publicStringgetSixMonthRate() {return sixMonthRate;}
    publicStringgetTwelveMonthRate() {return twelveMonthRate;}
    publicStringgetDateofFirstRegistrationegistration() {return dateofFirstRegistration;}    
    publicStringgetYearOfManufacture() {return yearOfManufacture;}
    publicStringgetCylinderCapacity() {return cylinderCapacity;}
    publicStringgetCo2Emissions() {return co2Emissions;}

    @SuppressLint("NewApi")
    publicvoidreadAndParseJSON(Stringin) {
        try {
            JSONObject reader = newJSONObject(in);
            make = reader.getString("make");
            model = reader.getString("model");
            sixMonthRate = reader.getString("sixMonthRate");
            twelveMonthRate = reader.getString("twelveMonthRate");
            dateofFirstRegistration= reader.getString("dateOfFirstRegistration");
            yearOfManufacture= reader.getString("yearOfManufacture");
            cylinderCapacity= reader.getString("cylinderCapacity");
            co2Emissions= reader.getString("co2Emissions");

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

Here is the output

enter image description here

References:

Solution 2:

Is DVLA JSON DATABASE supposed to be the contents of JSON_ARRAY?

If so then you should be calling getJsonObject instead of getJsonArray and then iterate through them as such:

for(String key : vehicle.keySet() {
    // Do something with the key and object.Object obj = vehicle.get(key);
}

If not, I guess I didn't fully understand your question

Edited

I'm not sure where you get your `json' value but I'm gonna assume it is the contents of DVLA_JSON_DATABASE

If not, just copy paste the following:

JSONObject jsonObject = new JSONObject("{\"make\":\"VOLKSWAGEN\",\"model\":\"Tiguan\",\"sixMonthRate\":\"Unknown\",\"twelveMonthRate\":\"Unknown\",\"dateOfFirstRegistration\":\"23 July 2009\",\"yearOfManufacture\":\"2009\",\"cylinderCapacity\":\"1968cc\",\"co2Emissions\":\"167 g/km\",\"fuelType\":\"DIESEL\",\"taxStatus\":\"Taxed and due\",\"colour\":\"SILVER\",\"typeApproval\":\"M1\",\"wheelPlan\":\"2 AXLE RIGID BODY\",\"revenueWeight\":\"Not available\",\"taxDetails\":\"Tax due: 01 April 2016\",\"motDetails\":\"Expires: 21 April 2016\",\"taxed\":true,\"mot\":true}");

in place of json

Now for some code:

try {
        jsonObject = newJSONObject(json);

        String make = jsonObject.getString("make");
        String model = jsonObject.getString("model");
        String sixMonthRate = jo.getString("sixMonthRate");
        // Do something with these variables

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

You might've noticed but a JSON consists of a key:value. You simply call getString(key) to get a String value. Similarly you would call 'getInt(key)' to get a Integer value

That's all there is to it!

Post a Comment for "Parsejson.java, Parsing Result If No Array Name?"