0

我有一个如下所示的 JSON 文件:

{
e:"_caIUfOJIo3FPeOwgOAI",
c:0,
u:"https://www.google.com.tr/complete/search?client\x3dhp\x26hl\x3den\x26sugexp\x3dernk_ir\x26gs_rn\x3d8\x26gs_ri\x3dhp\x26tok\x3dVepIjB5Y4hHneLHcIO5AsQ\x26cp\x3d9\x26gs_id\x3d1sy\x26xhr\x3dt\x26q\x3damerica%20is\x26tch\x3d1\x26ech\x3d1\x26psi\x3dFW9kUZu1J6Sw0QXLyIHgBg.1365536538389.1",
p:true,
d:"[
    \x22america is\x22,
        [
            [\x22america is\\u003Cb\\u003E doomed\\u003C\\/b\\u003E\x22,0,[]],
            [\x22america is\\u003Cb\\u003E in the heart\\u003C\\/b\\u003E\x22,0,[]],
            [\x22america is\\u003Cb\\u003E evil\\u003C\\/b\\u003E\x22,0,[]],
            [\x22america is\\u003Cb\\u003E not a christian nation\\u003C\\/b\\u003E\x22,0,[]],
            [\x22america is\\u003Cb\\u003E not the greatest country\\u003C\\/b\\u003E\x22,0,[]],
            [\x22america is\\u003Cb\\u003E in the heart summary\\u003C\\/b\\u003E\x22,0,[]],
            [\x22america is\\u003Cb\\u003E dying\\u003C\\/b\\u003E\x22,0,[]],
            [\x22america is\\u003Cb\\u003E falling apart\\u003C\\/b\\u003E\x22,0,[]],
            [\x22america is\\u003Cb\\u003E all about speed\\u003C\\/b\\u003E\x22,0,[]],
            [\x22america is\\u003Cb\\u003E in the heart sparknotes\\u003C\\/b\\u003E\x22,0,[]]
        ],
        {\x22j\x22:\x221sy\x22,\x22q\x22:\x22f87KpN3GEzebiGEgM8LiN37wsQc\x22,\x22t\x22:{\x22bpc\x22:false,\x22tlw\x22:false}}
    ]
    "}/*""*/

我想解析“注定”、“内心”、“邪恶”等字符串。但我对 Android 和 JSON 解析都是新手。所以请你检查一下我在解析这种 JSON 方面的进展。这是我的功能:

@SuppressWarnings("null")
    public String[] parseJSON(String URL)
    {
        String[] finals = null;
        JSONArray results = null;
        String TAG_RESULTS = "d";

        // Creating JSON Parser instance
        JSONParser jParser = new JSONParser();

        // getting JSON string from URL
        JSONObject json = jParser.getJSONFromUrl(URL);

        try {
            results = json.getJSONArray(TAG_RESULTS);
            JSONArray tenResults = results.getJSONArray(0);  

            for(int i = 0; i < tenResults.length(); i++){
                JSONArray c = tenResults.getJSONArray(i);
                String content = c.toString();
                content.substring(content.indexOf("\\u003Cb\\u003E ")+"\\u003Cb\\u003E ".length(), content.indexOf("\\u003C\\/b"));
                finals[i] = content;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return finals;
    } 

我尝试调试应用程序,但由于某些源文件丢失了我无法理解的错误,我无法进入 try 块。据我所见,它成功创建了JSONParser对象。

编辑: Logcat

05-07 12:17:02.562: E/AndroidRuntime(6007): FATAL EXCEPTION: main
05-07 12:17:02.562: E/AndroidRuntime(6007): java.lang.IllegalStateException: Could not execute method of the activity
05-07 12:17:02.562: E/AndroidRuntime(6007):     at android.view.View$1.onClick(View.java:3597)
05-07 12:17:02.562: E/AndroidRuntime(6007):     at android.view.View.performClick(View.java:4202)
05-07 12:17:02.562: E/AndroidRuntime(6007):     at android.view.View$PerformClick.run(View.java:17340)
05-07 12:17:02.562: E/AndroidRuntime(6007):     at android.os.Handler.handleCallback(Handler.java:725)
05-07 12:17:02.562: E/AndroidRuntime(6007):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-07 12:17:02.562: E/AndroidRuntime(6007):     at android.os.Looper.loop(Looper.java:137)
05-07 12:17:02.562: E/AndroidRuntime(6007):     at android.app.ActivityThread.main(ActivityThread.java:5039)
05-07 12:17:02.562: E/AndroidRuntime(6007):     at java.lang.reflect.Method.invokeNative(Native Method)
05-07 12:17:02.562: E/AndroidRuntime(6007):     at java.lang.reflect.Method.invoke(Method.java:511)
05-07 12:17:02.562: E/AndroidRuntime(6007):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-07 12:17:02.562: E/AndroidRuntime(6007):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-07 12:17:02.562: E/AndroidRuntime(6007):     at dalvik.system.NativeStart.main(Native Method)
05-07 12:17:02.562: E/AndroidRuntime(6007): Caused by: java.lang.reflect.InvocationTargetException
05-07 12:17:02.562: E/AndroidRuntime(6007):     at java.lang.reflect.Method.invokeNative(Native Method)
05-07 12:17:02.562: E/AndroidRuntime(6007):     at java.lang.reflect.Method.invoke(Method.java:511)
05-07 12:17:02.562: E/AndroidRuntime(6007):     at android.view.View$1.onClick(View.java:3592)
05-07 12:17:02.562: E/AndroidRuntime(6007):     ... 11 more
05-07 12:17:02.562: E/AndroidRuntime(6007): Caused by: android.os.NetworkOnMainThreadException
05-07 12:17:02.562: E/AndroidRuntime(6007):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
05-07 12:17:02.562: E/AndroidRuntime(6007):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
05-07 12:17:02.562: E/AndroidRuntime(6007):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
05-07 12:17:02.562: E/AndroidRuntime(6007):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
05-07 12:17:02.562: E/AndroidRuntime(6007):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
05-07 12:17:02.562: E/AndroidRuntime(6007):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-07 12:17:02.562: E/AndroidRuntime(6007):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-07 12:17:02.562: E/AndroidRuntime(6007):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
05-07 12:17:02.562: E/AndroidRuntime(6007):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-07 12:17:02.562: E/AndroidRuntime(6007):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-07 12:17:02.562: E/AndroidRuntime(6007):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-07 12:17:02.562: E/AndroidRuntime(6007):     at com.example.googlesearch.JSONParser.getJSONFromUrl(JSONParser.java:38)
05-07 12:17:02.562: E/AndroidRuntime(6007):     at com.example.googlesearch.AuxilaryVerbs.parseJSON(AuxilaryVerbs.java:39)
05-07 12:17:02.562: E/AndroidRuntime(6007):     at com.example.googlesearch.AuxilaryVerbs.is_clicked(AuxilaryVerbs.java:81)
05-07 12:17:02.562: E/AndroidRuntime(6007):     ... 14 more
05-07 12:17:02.572: D/dalvikvm(6007): GC_CONCURRENT freed 439K, 9% free 5623K/6176K, paused 3ms+0ms, total 6ms
4

4 回答 4

0

d:"[ ... ]"不是一个JSONArray而是String一个。

您需要获取该字符串并再次解析为 JSON。

于 2013-05-07T12:11:55.950 回答
0

为什么不让自己的生活更简单并使用Gson呢?根据您的代码,我敲了一个快速示例:

创建一个 POJO 来表示您的模型:

public class Result {

    @SerializedName("e")
    public String e;

    @SerializedName("c")
    public int c;

    @SerializedName("u")
    public String u;

    @SerializedName("p")
    public boolean p;

    @SerializedName("d")
    public String[] d;

}

现在从 Json 字符串创建一个实例

String json = "//some json string//";
Gson gson = new Gson();
Result result = gson.fromJson(json, Result.class);

//do something with result
if(result.p)
{
  //argh we have a "p"
}

//or maybe we iterate over the strings you were after
for(String myString : result.d)
{
    //do something with myString which might be set to "evil" etc
}
于 2013-05-07T12:07:09.003 回答
0

EDIT2 我几乎猜到了 - NetworkOnMainThreadException。在不同的线程上移动代码。我建议使用AsyncTask

编辑

如果应用程序在 try 块之前崩溃,那么您很可能错过了将此权限添加到清单中:

<uses-permission android:name="android.permission.INTERNET" /> 

另外,您需要更改此行

content.substring(content.indexOf("\\u003Cb\\u003E ")+"\\u003Cb\\u003E ".length(), content.indexOf("\\u003C\\/b"));

对此

content = content.substring(content.indexOf("\\u003Cb\\u003E ")+"\\u003Cb\\u003E ".length(), content.indexOf("\\u003C\\/b"));

该方法不会影响字符串的当前实例,但会返回一个新实例。

于 2013-05-07T12:06:20.220 回答
0

我认为您在该字符串中有问题:

String[] finals = null;

您不创建字符串数组,而是循环使用它:

finals[i] = content;

在这种情况下,我可以建议您使用ArrayList

List<String> finals = new ArrayList<String>();
...
finals.add(content);

而且你的方法可以返回List<String>,但是如果你需要数组,你可以转换它:

String[] array = new String[finals.size()];
finals.toArray(array);

return array;
于 2013-05-07T12:18:56.020 回答