Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:如何忽略JSON中的javascript变量?_Javascript_Android_Json - Fatal编程技术网

Android:如何忽略JSON中的javascript变量?

Android:如何忽略JSON中的javascript变量?,javascript,android,json,Javascript,Android,Json,我正在从包含JavaScript变量的JSON文件中检索数据。(我没有创建这个文件,也没有办法修改它) 我已经知道了如何使用JSONReader读取这些内容,但除非我在输入流上使用skip方法,否则我无法使用它,因为它们的值可以更改 我只想解析以下内容 [ { "01":"fred", "02":"mary", "03":"jake" } ] [ {"01" : "true", "02" : "false", "03" : "true", "04" : "false", "05" : "true

我正在从包含JavaScript变量的JSON文件中检索数据。(我没有创建这个文件,也没有办法修改它)

我已经知道了如何使用JSONReader读取这些内容,但除非我在输入流上使用skip方法,否则我无法使用它,因为它们的值可以更改

我只想解析以下内容

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

您可以使用答案中描述的方法,例如,创建如下方法:

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;
}
public List extractJSONStringsfromText(字符串文本、最终字符串symbolBegin、最终字符串symbolEnd){
int-ixbegingson=0;
int-ixEndJSON=0;
字符串textJSON;
List textsJSON=new ArrayList();
ixBeginJSON=text.indexOf(符号开始,ixBeginJSON+1);
做{
ixEndJSON=text.lastIndexOf(symbolEnd);
if(ixEndJSON ixBeginJSON);
ixBeginJSON=text.indexOf(符号开始,ixBeginJSON+1);
}而(IXBegingson!=-1);
返回文本;
}
你可以这样称呼它:

ArrayList<String> jsonLines = (ArrayList) extractJSONStringsfromText(text, "[", "]");
for (String jsonLine : jsonLines) {
    Log.d(TAG, "jsonLine: '" + jsonLine + "'");
}
ArrayList jsonLines=(ArrayList)extractJSONStringsfromText(text,“[”,“]);
for(字符串jsonLine:jsonLine){
Log.d(标记“jsonLine:”+jsonLine+“”);
}

您希望它如何解析?把它放进地图(字典)里?它实际上是一个javascript文件,而不是JSON对象/文件?为什么不像普通文本文件一样读取它,在“=”处拆分每一行,然后将右侧解析为JSONObject?
ArrayList<String> jsonLines = (ArrayList) extractJSONStringsfromText(text, "[", "]");
for (String jsonLine : jsonLines) {
    Log.d(TAG, "jsonLine: '" + jsonLine + "'");
}