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
   }
  }
]
Thomas Hu
  • 45
  • 1
  • 2
  • 9
  • 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 – Thomas Hu Nov 10 '15 at 20:29
  • @user3765602 can you post code that can process a single **Node**? – Alain O'Dea Nov 10 '15 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. – TayTay Nov 10 '15 at 21:05
  • I also didn't, but the obvious question style gaps are no executable code and not ending with a question. – Alain O'Dea Nov 10 '15 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?" – Alain O'Dea Nov 10 '15 at 21:09
  • I think this is a duplicate. The OP has two questions, apparently, 1) how to read a file and loop though its contents, and 2) how to parse json. The question however focuses on json, and as such is a duplicate of the answer linked in the comment above. – Software Engineer Nov 10 '15 at 21:13
  • that's fine ! it doesn't matter. I am sorry to misunderstand you. At least I have people answered this question. @alain – Thomas Hu Nov 10 '15 at 21:19
  • @user3765602 please accept my answer if it works for you. I use Gson in heavy production use (many GBs of messages per day) and it's much more convenient that javax.json. – Alain O'Dea Nov 10 '15 at 21:24
  • thank you so much for telling me about this, I am trying the code now. Will get back to you later. @AlainO'Dea – Thomas Hu Nov 10 '15 at 21:29
  • Thank you so much Alain, it worked !! Mwah !! @AlainO'Dea – Thomas Hu Nov 10 '15 at 21:44

2 Answers2

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...
    ....
    ....
}
Joel Min
  • 3,387
  • 3
  • 19
  • 38
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
        }
    }
}
Alain O'Dea
  • 21,033
  • 1
  • 58
  • 84
  • Very similar to another question, but with different objects: http://stackoverflow.com/questions/18421674/using-gson-to-parse-a-json-array – Alain O'Dea Nov 10 '15 at 20:52