Skip to content
Advertisement

String to JSONObject troubles

Is the first time I have to make this and I think I’m going to the wrong side. I trying to pass my String to a JSONObject but it’s nothing working. My code is this:

String locationStringJson = farm.getCommonAreas().get(0).getLocation().replaceAll("\\", "");
                                    try {
                                        JSONObject locationObj = new JSONObject(locationStringJson);
                                        Log.d("My App", locationObj.toString());

                                    } catch (Throwable t) {
                                        Log.e("My App", "Could not parse malformed JSON: "" + locationStringJson + """);
                                    }

The locationObj value after:

{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-46.19679305516182,-21.3557873035952,875.7922817609483],[-46.19509985061597,-21.35461892353634,851.7199664001099],[-46.19352440512642,-21.35465468553253,830.544107896781],[-46.19210971746411,-21.35533178647862,834.4995579747846],[-46.19246633589201,-21.35325655623514,818.0053650355306],[-46.19056278024465,-21.35223771617781,814.4561932463444],[-46.19485426724801,-21.34927033959156,820.8368637750491],[-46.19406925297564,-21.34411169376047,835.869714331503],[-46.19383528445265,-21.34257184056689,846.0504330417629],[-46.19484350479828,-21.34182697538793,856.2311517520229],[-46.1962878784062,-21.34151208337897,853.2060549203649],[-46.19720802861893,-21.34212719882432,863.5767764476165],[-46.19799767515288,-21.34318122502575,872.7086576897467],[-46.19824820477749,-21.34470459858373,883.3796232608263],[-46.19909510449183,-21.34436858896095,878.2872975189589],[-46.19956076082133,-21.34528105975711,869.8407434658429],[-46.20088312493932,-21.3453798405374,859.443203762798],[-46.2002428481787,-21.34812665616198,844.5221403864139],[-46.1987871829488,-21.34973816454015,824.467785524764],[-46.2044190023153,-21.35358726214673,896.3662262563042],[-46.20506039198465,-21.35408860964525,897.8948471808142],[-46.20532057690928,-21.35511888769405,899.4234681053241],[-46.20541331912865,-21.35620272644546,900.8867965073521],[-46.20461209029964,-21.3574176131669,902.0945075082227],[-46.20399199591689,-21.35775448256089,892.599969822067],[-46.1984691069506,-21.35606738619609,878.2315580520376],[-46.19679305516182,-21.3557873035952,875.7922817609483]]]},"properties":{"name":"Areado01","styleUrl":"#__managed_style_07990BBE71133E4DE97F","styleMapHash":{"normal":"#__managed_style_1F6694D867133E4DE97F","highlight":"#__managed_style_0817ADA964133E4DE982"}},"id":"03F80C78A7133E4C22CA"}]}

enter image description here

(The code continue like a String and I don’t know why)

The class I’m going to use to map later is this

public class FarmGeoJson {

    @SerializedName("type")
    @Expose
    private String type;
    @SerializedName("features")
    @Expose
    private List<Feature> features = null;

    public FarmGeoJson() {
        super();
    }

    public FarmGeoJson(String type, List<Feature> features) {
        this.type = type;
        this.features = features;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public List<Feature> getFeatures() {
        return features;
    }

    public void setFeatures(List<Feature> features) {
        this.features = features;
    }

    @Override
    public String toString() {
        return "FarmGeoJson{" +
                "type='" + type + ''' +
                ", features=" + features +
                '}';
    }
}

How can I convert my String locationStringJson to a FarmGeoJson.class?

Advertisement

Answer

Try the below method.

FarmGeoJson farmGeoJson = com.alibaba.fastjson.JSONObject.parseObject(locationStringJson, FarmGeoJson.class);
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement