0

I have a large stream of data that I can capture from a game that I play using CharlesProxy. I'd like to parse the data and have it print out (eventually build an excel spreadsheet) the player names, x and y location, and the guild name.

The JSON data in Paste-Bin (you'll have to go down a few entries to see one of the results that actually returns a player name as well): http://pastebin.com/v4kAaspn

Here's an example I found here that I tried to use to just return the player name, but I get a Null Pointer Exception error. Any advice will be greatly appreciated, thank you so much!

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class ToolMain {

    public static void main(String[] args) {

        JSONParser parser = new JSONParser();

        try {

            Object obj = parser.parse(new FileReader(
                    "//Users//Brandon//Desktop//JSONData.JSON"));

            JSONObject jsonObject = (JSONObject) obj;
            //get responses
            JSONArray rsp = (JSONArray)jsonObject.get("responses");
            //System.out.println(rsp);

            //get return value
            JSONObject rtvalue = (JSONObject)rsp.get(0);
            //System.out.println(rtvalue);

            //get hexes object
            JSONObject hexes = (JSONObject)rtvalue.get("return_value");
            //System.out.println(hexes);

            //get hexes array
            JSONArray hexesArray = (JSONArray)hexes.get("hexes");
            Iterator<JSONObject> iterator = hexesArray.iterator();
            while (iterator.hasNext()) {
                JSONObject factObj = iterator.next();
                String playerName = (String) factObj.get("player_name");
                if (playerName != null) {
                    System.out.println(playerName);
                }
            }
            //System.out.println(hexesArray);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

}
Brandon
  • 3
  • 3

1 Answers1

0

The NullPointerException is happening on below line because your JSONArray msg is null:

Iterator<JSONObject> iterator = msg.iterator();

Apply a check if(msg is not null) before you create an iterator on it.

Try it this way:

JSONObject jsonObject = (JSONObject) obj;
//get responses
JSONArray rsp = (JSONArray)jsonObject.get("responses");
System.out.println(rsp);

//get return value
JSONObject rtvalue = (JSONObject)rsp.get(0);
System.out.println(rtvalue);

//get hexes object
JSONObject hexes = (JSONObject)rtvalue.get("return_value");
System.out.println(hexes);

//get hexes array
JSONArray hexesArray = (JSONArray)hexes.get("hexes");
System.out.println(hexesArray);
LittlePanda
  • 2,496
  • 1
  • 21
  • 33
  • I added a while (msg != null) to it, and it runs without an error now. However it's still not returning any player names. Am I doing something incorrectly? This is my first time working with JSON. Thank you so much! – Brandon Apr 04 '15 at 14:16
  • Yes, you have to first select "responses" and then "return_value" and then "hexes", but I am trying to look up a way to directly look up "hexes" – LittlePanda Apr 04 '15 at 14:18
  • Thank you so much Panda for helping out, I'm looking to on how to access a "nested JSON object". At least that's what I think it may be called? Could be wrong! – Brandon Apr 04 '15 at 14:30
  • Your welcome, even I have less experience with this parser but you one has to go through the heirarchy to select the array - see this - http://stackoverflow.com/questions/1568762/accessing-members-of-items-in-a-jsonarray-with-java – LittlePanda Apr 04 '15 at 14:33
  • I went through those posts, I still can't quite seem to get it working. Any advice? – Brandon Apr 04 '15 at 14:50
  • I havent seen the Json response that well, you just need to keep moving down the heirarchy until you reach the object or array you want – LittlePanda Apr 04 '15 at 15:00
  • I got it completely working now. Thank you again SO much Panda. You're a genius and I'm really grateful you were able to help out. Thanks again! – Brandon Apr 04 '15 at 15:04