1

I am now want to read a series of JSON data(Nodes data) from local txt file(shown below, NODES.txt ). I use javax.json to do this.

Currently, I have a Node class which contains the attributes:type, id, geometry class(contains type, coordinates), properties class (contains name);

Here is the data I need to retrieve, it contains more than 500 nodes in it, here I just list 3 of them, so I need to use a loop to do it, I am quite new to this, please help !!

The sample JSON data in NODES.txt

[
 {
   "type" : "Feature",
   "id" : 8005583,
   "geometry" : {
    "type" : "Point",
    "coordinates" : [
     -123.2288,
     48.7578
    ]
   },
   "properties" : {
    "name" : 1
   }
  },
  {
   "type" : "Feature",
   "id" : 8005612,
   "geometry" : {
    "type" : "Point",
    "coordinates" : [
     -123.2271,
     48.7471
    ]
   },
   "properties" : {
    "name" : 2
   }
  },
  {
   "type" : "Feature",
   "id" : 8004171,
   "geometry" : {
    "type" : "Point",
    "coordinates" : [
     -123.266,
     48.7563
    ]
   },
   "properties" : {
    "name" : 3
   }
  },
     ****A Lot In the Between****
{
   "type" : "Feature",
   "id" : 8004172,
   "geometry" : {
    "type" : "Point",
    "coordinates" : [
     -113.226,
     45.7563
    ]
   },
   "properties" : {
    "name" : 526
   }
  }
]
10
  • it is very different from that one, I checked that one already. this one needs to use a LOOP, but there are no examples look like mine, why you deducted my score??? ! @Tgsmith61591 Commented Nov 10, 2015 at 20:29
  • @user3765602 can you post code that can process a single Node? Commented Nov 10, 2015 at 21:02
  • @user3765602 I didn't downvote you. People downvote if they feel the question does not add to the site or if it does not adhere to the rules for questions. If you don't include enough info in your question, people will dock you. Commented Nov 10, 2015 at 21:05
  • I also didn't, but the obvious question style gaps are no executable code and not ending with a question. Commented Nov 10, 2015 at 21:07
  • Also the question title should clearly differentiate it from similar questions. Perhaps a title like "How do I parse a JSON array from a file to POJOs?" Commented Nov 10, 2015 at 21:09

2 Answers 2

4

First, read in the file and store the content in ONE String:

BufferedReader reader = new BufferedReader(new FileReader("NODES.txt"));
String json = "";
try {
    StringBuilder sb = new StringBuilder();
    String line = reader.readLine();

    while (line != null) {
        sb.append(line);
        sb.append("\n");
        line = reader.readLine();
    }
    json = sb.toString();
} finally {
    reader.close();
}

Then parse the Json data from the String:

JSONObject object = new JSONObject(json); // this will get you the entire JSON node
JSONArray array = object.getJSONArray("Node"); // put in whatever your JSON data name here, this will get you an array of all the nodes

ArrayList<Node> nodeList = new ArrayList(array.length())<Node>;
for(int i=0; i<array.length(); i++){ // loop through the nodes
    JSONObject temp = array.getJSONObject(i);
    nodeList.get(i).setType(temp.getString("type")); //start setting the values for your node...
    ....
    ....
}
Sign up to request clarification or add additional context in comments.

Comments

0

Create classes to represent the entries:

Feature.java:

import java.util.Map;

public class Node {
    public String type;
    public String id;
    public Geometry geometry;
    public Properties properties;
}

Geometry.java:

import java.util.List;

public class Geometry {
    public String type;
    public List<Double> coordinates;
}

Properties.java:

public class Properties {
     public String name;
}

And an application main class to drive the processing.

Main.java:

import com.google.gson.Gson;
import java.io.FileReader;
import java.io.Reader;

public class Main {
    public static void main(String[] args) throws Exception {
        try (Reader reader = new FileReader("NODES.txt")) {
            Gson gson = new Gson();
            Node[] features = gson.fromJson(reader, Node[].class);
             // work with features
        }
    }
}

1 Comment

Very similar to another question, but with different objects: stackoverflow.com/questions/18421674/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.