0

This is a basic Java question I think that I can't work out how to get around.

I get data from Google Analytics API and store the rows in my database as a string as a JSONArray

[["New Zealand","Auckland","1640","8.795731707317072","516.4469512195122"],["New Zealand","Wellington","1314","8.428462709284627","580.3302891933029"]]

For Google Maps I need a JSON Array:

function drawMap() {
      var data = google.visualization.arrayToDataTable([
        ['City', 'Popularity'],
        ['New York', 200],
        ['Boston', 300],
        ['Miami', 400],
        ['Chicago', 500],
        ['Los Angeles', 600],
        ['Houston', 700]
      ]);

From https://developers.google.com/chart/interactive/docs/gallery/geomap

I need to change my data, by parsing it and iterating through it to remove the first (i.e. "New Zealand") and last variable from each object - I also need to add the headers i.e. ['City', 'Popularity']

Using GSonBuilder I can create JSON

[{"city":"Wellington","sessions":"1314","viewsPerSessions":"8.428462709284627","avgDuration":"8.428462709284627"},{"city":"Christchurch","sessions":"432","viewsPerSessions":"10.127314814814815","avgDuration":"10.127314814814815"}]

How do I turn that into a JSON Array?

4
  • It is JSON!, anyway, what's your question? you have data and a method you want to call, what's the problem? Explain better your problem Commented Jun 10, 2015 at 8:34
  • I need to change my data, by parsing it and iterating through it to remove the first (i.e. "New Zealand") and last variable from each object - I also need to add the headers i.e. ['City', 'Popularity'] Commented Jun 10, 2015 at 8:38
  • Ok, your question it's "how to parse this info"? if yes, please edit your question to explain better your question, not on comments. If this is your problem i will help you to parse a JSON file Commented Jun 10, 2015 at 8:41
  • I can already parse it I'm using JSONSerializer. This isn't the problem.. it's recreating the JSON that's correct for Google maps. Commented Jun 10, 2015 at 8:43

1 Answer 1

1

I use the JSON parse of Android. With this you can get what you want.

Try this:

JSONArray js_data = [["New Zealand","Auckland","1640","8.795731707317072","516.4469512195122"],["New Zealand","Wellington","1314","8.428462709284627","580.3302891933029"]];
int lenght = js_data.length();
JSONArray city;
for(int i=0;i<length;i++) {
    //get each city
    city = js_data.getJSONArray(i);
    String nameCity = city.getString(0);
    String pop = city.getString(4);
    //Create a object JSON or whatever you want with this data
    JSONArray js_array = new JSONArray();       
    js_array.put(city); js_array.put(pop);
    //And put on a list
    js_map.put(js_array);
}

This is a basic java coding. Hope it's helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.