Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have been trying, and struggling, to parse arrays contained in a JSON file, into a Java ArrayList. Could I please have some advise on how to proceed with this? Please find an example JSON Array below:

[
  {

    "id": "USER_ID_HERE",
    "dateTime": "DATE_TIME_HERE",
    "message": "WRITE_SOMETHING_MEANINGFUL_HERE",
    "latitude":  27.99999
    "longitude": 26.33333333
  }
]

My current Java code can be found below - how can I parse in the array from the JSON file?

public class ParseJSON {

ArrayList <Tweet> = new ArrayList <Tweet>();

class Tweet{
public String id;
public String message;
public String dateTime;
public double latitude;
public double longitude;
} 

JSONObject js = new JSONObject("tweets");

JSONArray jsarray = js.getJSONArray("tweets");
int size = jsarray.length();
for (int i = 0; i < size; i++)
{
    JSONObject tweet = jsarray.getJSONObject(i);
    System.out.println("tweet: " + tweet.getString("id"));
    System.out.println("tweet: " + tweet.getString("message"));
    System.out.println("tweet: " + tweet.getString("dateTime"));
    System.out.println("tweet: " + tweet.getDouble("latitude"));
    System.out.println("tweet: " + tweet.getDouble("longitude"));
}

   t = new tweet( tweet.getString("id")) & ( tweet.getString("message")) & ( tweet.getString("datetime")) & ( tweet.getDouble("latitude")) &    ( tweet.getDouble("longitude"));

  Tweet.list.add(t);

 }
}

Thank you in advance for any advice.

share|improve this question
    
This can be helpfull for you : stackoverflow.com/questions/3395729/… –  Semih Eker Nov 26 at 21:08
    
The JSON sample above is an "array" containing an "object". (See json.org for the syntax.) It maps to a Java List containing a Map. –  Hot Licks Nov 26 at 21:29
    
can you specify what is the exception / behavior that you currently have? what are you trying to achive? (print to screen, store in objects.. etc) –  ymz Nov 26 at 21:44
    
I am unsure how to parse the JSON file into Eclipse - what is the array equivalent of parsing an object (Object obj = parser.parse(new FileReader("insert file location"));)? –  Chipotle Nov 26 at 21:49
    
I will be using the data that is parsed in with Sentiwordnet in order to gather values for tweets, to determine whether they are negative or positive –  Chipotle Nov 26 at 21:50

1 Answer 1

You seem to be pretty close to the required functionality, some issues though:

Firstly, the code you want to execute is not in a method. Try to add a "main" method to your ParseJSON class.

Your Tweet class doesn't have a custom constructor, so you won't be able to create Tweet objects and populate their attributes with a constructor as you attempt to do further down.

Your ArrayList<Tweet> member variable doesn't have an identifier.

For:

t = new tweet( tweet.getString("id")) & ( tweet.getString("message")) & ( tweet.getString("datetime")) & ( tweet.getDouble("latitude")) &    ( tweet.getDouble("longitude"));

You want to do this for each element of the JSON array, so move the line inside your for loop. Also, the parameters should be separated by a comma, not an ampersand. You should also use a capital T for new Tweet (to refer to the constructor you should have created).

For:

Tweet.list.add(t);

This currently expects list to be a (static) member of Tweet. Try changing this to access the name you should have given to your ArrayList. This line should also be inside the for loop for the same reason as above.

share|improve this answer
1  
Just for the record, in Java, empty constructors are not mandatory. Java provides an empty one by default if not defined. –  Mati Cicero Nov 26 at 22:08
    
Indeed, but further down there is an attempt at using a non-default constructor. You're right I will be clearer on that. –  James Nov 26 at 22:10
    
Can't manage to find the piece of code you are talking about, but it was a FYI for the OP nevertheless –  Mati Cicero Nov 26 at 22:11
1  
Near the bottom: t = new tweet( tweet.getString("id")) & ( tweet.getString("message")) & ( tweet.getString("datetime")) & ( tweet.getDouble("latitude")) & ( tweet.getDouble("longitude")); –  James Nov 26 at 22:12
    
Oh.. Isn't tweet a JSONObject? –  Mati Cicero Nov 26 at 22:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.