Remixt
Remixt

Reputation: 597

Java REGEX .replaceall() not working on specific JSON object (converted to string)

I'm trying to remove all the slashes out of a JSON Object and then add it to an array list. Here is what it looks like regardless of what I type into Regex: I also want to remove the brackets and duplicate strings, but even trying to remove the slashes isn't working so I'm trying to build it up. Just as a test I've tried replacing anything and everything in the String, but no matter what I pick (even alpha numerics) it doesn't work.

Example output for an item in the category list: "[[\"Mediterranean\",\"mediterranean\"],[\"Greek\",\"greek\"]]"

private static final ArrayList<String> BusinessIDList = new ArrayList<>();
private static ArrayList<String> RatingList = new ArrayList<>();
private static ArrayList<String> cityList = new ArrayList<>();
private static ArrayList<String> stateList = new ArrayList<>();
private static ArrayList<String> addressList = new ArrayList<>();
private static ArrayList<String> categoryList = new ArrayList<>();
private static ArrayList<String> phoneList = new ArrayList<>();
private static ArrayList<String> nameList = new ArrayList<>();

    private static void queryAPI(YelpAPI yelpApi, YelpAPICLI yelpApiCli) {

    String searchResponseJSON = yelpApi.searchForBusinessesByLocation(yelpApiCli.term, yelpApiCli.location);

    JSONParser parser = new JSONParser();
    JSONObject response = null;
    try {
        response = (JSONObject) parser.parse(searchResponseJSON);
    } catch (ParseException pe) {
        System.out.println("Error: could not parse JSON response:");
        System.out.println(searchResponseJSON);
        System.exit(1);
    }


    JSONArray businesses = (JSONArray) response.get("businesses");

    for (int i = 0; i < businesses.size(); i++) {
        JSONObject temp = (JSONObject) businesses.get(i);
        JSONObject temp1 = (JSONObject) temp.get("location");

        BusinessIDList.add(temp.get("id").toString());
        RatingList.add(temp.get("rating").toString());
        cityList.add(temp1.get("city").toString());
        stateList.add(temp1.get("state_code").toString());
        phoneList.add(temp.get("phone").toString());

        String tempCat = temp.get("categories").toString();
        tempCat = tempCat.replaceAll("\\\\", " ");
        categoryList.add(tempCat);

        String tempAdr = temp1.get("address").toString();
        tempAdr = tempAdr.replaceAll("\\\\", " ");
        addressList.add(tempAdr);

        nameList.add(temp.get("name").toString());

    }

Upvotes: 0

Views: 840

Answers (1)

user2170842
user2170842

Reputation:

You're not assigning tempCat to the return value of replaceAll. Try changing:

tempCat.replaceAll("\\\\\\\\", " ");

to

tempCat = tempCat.replaceAll("\\\\\\\\", " ");

For more information about the replaceAll method, see: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceAll(java.lang.String,%20java.lang.String)

Edit: Sorry, updated with the correct link.

Upvotes: 3

Related Questions