-1

I am retrieving data from a JSON file that has JavaScript variables included.(I did not make this file and there is no way for me to modify it)

var name = [ { "01":"fred", "02":"mary", "03":"jake" } ]
var day = [ {"01" : "true", "02" : "false", "03" : "true", "04" : "false", "05" : "true", "06" : "true"}]

I have figured out how to read this with a JSONReader but am not able to unless I use the skip method on the input stream, which I can't use because they values can change.

I want to just get the following to parse

[ { "01":"fred", "02":"mary", "03":"jake" } ]
[ {"01" : "true", "02" : "false", "03" : "true", "04" : "false", "05" : "true", "06" : "true"}]
  • How do you want it to parse? Into a map (dictionary)? Into an object? – Sumner Evans Oct 31 '16 at 02:29
  • so it's actually a javascript file, not a JSON object/file? why not read it like a regular text file, split each line at the "=", then parse the right-hand side as a JSONObject? – Gino Mempin Oct 31 '16 at 04:03

1 Answers1

0

You can use approach, described in this answer, fore example, create method like that:

public List<String> extractJSONStringsfromText(String text, final String  symbolBegin, final String  symbolEnd) {
    int ixBeginJSON = 0;
    int ixEndJSON = 0;
    String textJSON;
    List<String> textsJSON = new ArrayList<>();

    ixBeginJSON = text.indexOf(symbolBegin, ixBeginJSON + 1);

    do {
        ixEndJSON = text.lastIndexOf(symbolEnd);
        if(ixEndJSON <= ixBeginJSON) {
            break;
        }

        do {
            textJSON = text.substring(ixBeginJSON, ixEndJSON + 1);
            try {
                if (symbolBegin.equals("[")) {
                    new JSONArray(textJSON);
                } else {
                    new JSONObject(textJSON);
                }
                textsJSON.add(textJSON);
            } catch (Exception ignore) {
            }
            ixEndJSON = text.substring(0, ixEndJSON).lastIndexOf(symbolEnd);
        } while(ixEndJSON > ixBeginJSON);

        ixBeginJSON = text.indexOf(symbolBegin, ixBeginJSON + 1);

    } while(ixBeginJSON != -1);

    return textsJSON;
}

And You can call it like this way:

ArrayList<String> jsonLines = (ArrayList) extractJSONStringsfromText(text, "[", "]");
for (String jsonLine : jsonLines) {
    Log.d(TAG, "jsonLine: '" + jsonLine + "'");
}
Community
  • 1
  • 1
Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79