0

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());

    }
7
  • Let's keep this simple; String t="[[\\\"Med\\\",\\\"med\\\"]"; t = t.replaceAlll("\\\\",""); System.out.println(t); Commented Oct 22, 2016 at 22:18
  • I cannot reproduce your problem, however your code is incomplete and cannot be run on its own. Could you please post a minimal reproducible example that reproduces the problem? Commented Oct 22, 2016 at 22:19
  • Sure one sec, sorry about that. Commented Oct 22, 2016 at 22:19
  • The new code is certainly not minimal nor complete. If you want people to help you, you must provide code and input data that make it (easily) runnable to reproduce the problem. Please read the minimal reproducible example help page carefully. Commented Oct 22, 2016 at 22:35
  • 1
    What I'm saying is, only you can fix your code. You ask a question about why a pattern is not matching or replacing as expected, and you show an example where we see a pattern, and you tell us what the input is but then claim the output is not changed. Well. Just try one line. All by itself. System.out.println("[[\\\"Iknow\\\",\\\"whatImDoing\\\"]]".replaceAll("\\\\","")); I think you'll find your problem has nothing to do with the pattern, nor replaceAll but is somewhere else. Commented Oct 23, 2016 at 0:03

1 Answer 1

3

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.

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

1 Comment

No cigar on that one. Sorry I had deleted that earlier to try it as a possible solution.

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.