0

I am making a program which gets the Json distance matrix from Google Maps API. So far i wrote the program can get the json it looks as below

  {
   "destination_addresses" : [
      "Paris, France",
      "Toulon, France",
      "Nantes, France",
      "Marseille, France"
   ],
   "origin_addresses" : [
      "Paris, France",
      "Toulon, France",
      "Nantes, France",
      "Marseille, France"
   ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "1 m",
                  "value" : 0
               },
               "duration" : {
                  "text" : "1 min",
                  "value" : 0
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "839 km",
                  "value" : 838906
               },
               "duration" : {
                  "text" : "7 hours 21 mins",
                  "value" : 26476
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "384 km",
                  "value" : 384421
               },
               "duration" : {
                  "text" : "3 hours 34 mins",
                  "value" : 12823
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "774 km",
                  "value" : 774398
               },
               "duration" : {
                  "text" : "6 hours 48 mins",
                  "value" : 24490
               },
               "status" : "OK"
            }
         ]
      },

Now what i want to do is to create a distance matrix as this

       [ Paris        Toulon     Nantes         Marseille ]
[Paris       0           838906     384421           774398 ]
[Toulon    value            0       value            value]
[Nantes    value          value       0              value] 
[Marseille value          value      value             0 ]

** The values stands for distance between the cities.

(in java form of course) and later i will use it in solving the TSP. I need the values from Elements --> Distance --> Values. Any help would be appreciated. Thanks in advance.

Sujith PS
  • 4,776
  • 3
  • 34
  • 61

4 Answers4

1

You can use org.codehaus.jettison.json.JSONArray for the same (Download jar).

Example Code :

   JSONObject json = new JSONObject(result_string);
   JSONArray rows  = json .getJSONArray("rows");

Creating distance matrix for TCP problem :

You can use the following code :

            JSONObject json1 = new JSONObject(json);
            String places[] ={"Paris"   ,    "Toulon"  ,   "Nantes"    ,     "Marseille" };
            String[][] result=new String[places.length][places.length];
            JSONArray rows  = json1.getJSONArray("rows");
            //JSONObject[] elements=new JSONObject[places.length];
            for (int i = 0; i < rows.length(); i++) {
                JSONObject obj=rows.getJSONObject(i);
                JSONArray elements=obj.getJSONArray("elements");
                for (int j = 0; j < elements.length(); j++) {
                    JSONObject elem=elements.getJSONObject(j);
                    JSONObject distance = elem.getJSONObject("distance");
                    result[i][j]=distance.getString("value");
                }

            }
            for (int i = 0; i < result.length; i++) {
                String row[]=result[i];
                for (int j = 0; j < row.length; j++) {
                    System.out.print("  " +row[j] + " , ");
                }
                System.out.println();
            }

Like this you can get values of json string .

Sujith PS
  • 4,776
  • 3
  • 34
  • 61
0

When working with JSON we usually use libraries to make it easier, someone else has done the hard work to parse all that information.

GSON is one https://code.google.com/p/google-gson/ and there are plenty others if you look around.

Once you have a library, give it your json and then you can start manipulating it. You will be able to get rows -> element -> duration -> text quite easily.

This should get your started: http://www.javacreed.com/simple-gson-example/

From there you should write code to take your GSON and put in a datas tructure that makes it easy to work with like a binary tree or something similar.

Remember libraries are your friend.

ddoor
  • 5,819
  • 9
  • 34
  • 41
  • I downloaded the GSON from the website, watched a video how to add the library but still not sure completely how to add it to my project. Could you tell me what to do ? Or suggest where i can find it step by step ? – Inemzireno Jan 07 '14 at 06:46
  • You need to add it as a library ususally, google add GSON to eclipse or something like that. – ddoor Jan 07 '14 at 06:46
  • I added the library (am using eclipse btw) but when i import "import com.google.gson.Gson;" it gives an error such as "- The import com.google cannot be resolved" Any info what might went wrong ? – Inemzireno Jan 07 '14 at 07:01
0

you can use Java Api for JSON.

you can start with : http://www.oracle.com/technetwork/articles/java/json-1973242.html

djavaphp
  • 68
  • 7
0

download and install "json.simple".

add maven dependency in pom.xml file.

  <dependency>  
        <groupId>com.googlecode.json-simple</groupId>  
        <artifactId>json-simple</artifactId>  
        <version>1.1</version>  
      </dependency>

You can also refer this site