Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8594411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:11:39+00:00 2026-06-12T00:11:39+00:00

I want to create Json object by getting points of a shape file in

  • 0

I want to create Json object by getting points of a shape file in while loop.The shape file contains 73 points.I am storing points of shape file in json oject and adding the object to ArrayList in a loop,below the while loop i m retrieving objects one by one from ArrayList and storing it in JsonArray, but the problem here is only the last record points of shapefile are storing in the arraylist 73 times.How to solve this problem please help me.

Here is my code

try {

        File file= new File(Environment.getDataDirectory().getAbsolutePath()+"/ne_50m_admin_0_tiny_countries.shp");
        Long fileLength=file.length();
        Log.d("FileLength is","file"+fileLength);
        String shpFile = Environment.getDataDirectory().getAbsolutePath().toString()+"/ne_50m_admin_0_tiny_countries.shp";
        //String shpFile = Environment.getDataDirectory().getAbsolutePath().toString()+"/ne_50m_admin_0_breakaway_disputed_areas.shp";
        ShapeReader reader = new ShapeReader(shpFile, true);
        ShapeType shpType=reader.getHeader().getShapeType();
        JSONObject jObject = new JSONObject();
        JSONObject wkidJson = new JSONObject();
        jObject.put("objectIdFieldName","OBJECTID");
        jObject.put("globalIdFieldName","");
        jObject.put("geometryType",shpType);
        wkidJson.put("wkid", new Integer(102100));
        jObject.put("spatialReference",wkidJson);
        JSONArray fieldsList = new JSONArray();
        JSONObject idJson = new JSONObject();
        JSONObject yJson = new JSONObject();
        JSONObject xJson = new JSONObject();
        idJson.put( "name","OBJECTID");
        idJson.put("alias", "OBJECTID");
        idJson.put("type", "esriFieldTypeOID");
        yJson.put( "name","y");
        yJson.put("alias", "y");
        yJson.put("type", "esriFieldTypeDouble");
        xJson.put( "name","x");
        xJson.put("alias", "x");
        xJson.put("type", "esriFieldTypeDouble");
        fieldsList.put(idJson);
        fieldsList.put(yJson);
        fieldsList.put(xJson);
        jObject.put("fields",fieldsList);
        JSONObject geomJson = new JSONObject();
        JSONObject attJson = new JSONObject();
        JSONObject featuresJson = new JSONObject();
        JSONArray featuresList = new JSONArray();
        Log.d("Header info is","Header"+reader.getHeader().getFileLength());
        recordshape=reader.new Record();
        ArrayList a= new ArrayList();
        while (reader.hasNext())
        {

            if(recordshape.end < fileLength)
            {
                if(shpType==ShapeType.POINT)
                {
                    recordshape=reader.nextRecord();
                    int recordNumber=recordshape.number;
                    double Xcoordinate= recordshape.minX;
                    double Ycoordinate=recordshape.minY;
                    Log.d("Xcoordinate info is","Xcoordinate"+Xcoordinate);
                    Log.d("Ycoordinate info is","Ycoordinate"+Ycoordinate);
                    geomJson.put("x", Xcoordinate);
                    geomJson.put("y", Ycoordinate);
                    attJson.put("OBJECTID", recordNumber);
                    attJson.put("y", Ycoordinate);
                    attJson.put("x", Xcoordinate);
                }

            }
            else
                break;

            featuresJson.put("geometry", geomJson);
            featuresJson.put("attributes", attJson);
            a.add(featuresJson);
            Log.d("featuresJson info is","featuresJson"+featuresJson);
         }
        Log.d("featuresJson info is","featuresJson"+featuresJson);
        Log.d("size info is","size"+a.size());
        for(int i=0;i<a.size();i++)
        {
        featuresList.put(a.get(i));
        }

        Log.d("featuresList info is","featuresList"+featuresList);
        jObject.put("features",featuresList);
        String jsonString = jObject.toString();

        jsonString = jsonString.replace("\\" , "");

        FileWriter writeFile = new FileWriter(Environment.getDataDirectory().getAbsolutePath().toString()+"/Test.json");
        BufferedWriter out = new BufferedWriter(writeFile);
        out.write(jsonString);
        out.flush();
        out.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.d("Error","Message"+e.getMessage());
    }catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
            Log.d("Exception","Message"+e.getMessage());
    }

The format of json which i want is

{
"objectIdFieldName": "OBJECTID",
"globalIdFieldName": "",
"geometryType": "esriGeometryPoint",
"spatialReference": {
    "wkid": 102100
},
"fields": [
    {
        "name": "OBJECTID",
        "alias": "OBJECTID",
        "type": "esriFieldTypeOID"
    },
    {
        "name": "y",
        "alias": "y",
        "type": "esriFieldTypeDouble"
    },
    {
        "name": "x",
        "alias": "x",
        "type": "esriFieldTypeDouble"
    }
],
"features": [
    {
        "geometry": {
            "x": -13021472.272599999,
            "y": 4046325.7190999985
        },
        "attributes": {
            "OBJECTID": 212,
            "y": 4046325.7191,
            "x": -13021472.2726
        }
    },
    {
        "geometry": {
            "x": -13021124.9134,
            "y": 4046747.3976000026
        },
        "attributes": {
            "OBJECTID": 232,
            "y": 4046747.3976,
            "x": -13021124.9134

        }
    }
]

}

please help me
Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:11:41+00:00Added an answer on June 12, 2026 at 12:11 am

    in your loop, you put values in the same json object over and over again. therefore, when you jsonify it, your array contains n*the same object.

    You need to create new object to be added in each pass of your loop.

    these three declaration must move inside the loop:

    JSONObject geomJson = new JSONObject();
    JSONObject attJson = new JSONObject();
    JSONObject featuresJson = new JSONObject();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create JSON object using tow query in PHP , Then retrieve
I want to create a JSON object from my MySQL results with PHP so
This the model that I want to create using json file Ext.define('Users', { extend:
I want to be able to dynamically create ViewControllers based on a JSON file.
I want to create a hashtable of json objects, where each json object represents
I want to create JSON object from JSON data receive from server as A
I'm trying to create a json object from MySQL results, but not getting the
I want to create a json object in jquery and process it in jsp
I want to create a JSON string from a javascript for loop. This is
HI I am trying to create a JSON file in which I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.