如何将String转换为JsonArray?

时间:2012-07-03 14:02:35

标签: java android json string

我试图操纵JsonArray看起来像另一个JsonArray。 我为现有的JsonArray实现了一些字符串方法。现在我想把它放回另一个JsonArray中。

CODE

                Log.d("defaultArrayShuffled", defaultArray.toString());

                String temp = defaultArray.toString();
                String temp1= temp.replaceAll("_white_", "_");
                Log.d("temp1", temp1);

                JSONObject temp2 = new JSONObject();
                temp2.put("arr", temp1);
                Log.d("temp2", temp2.toString());
                replacingArray = temp2.getJSONArray("arr");

logcat的

07-03 19:16:37.310: D/defaultArrayShuffled(8863): ["findhidden\/o_white_64.png","findhidden\/x_white_64.png","findhidden\/f_white_64.png","findhidden\/b_white_64.png","findhidden\/a_white_64.png","findhidden\/g_white_64.png","findhidden\/p_white_64.png","findhidden\/u_white_64.png","findhidden\/n_white_64.png","findhidden\/j_white_64.png","findhidden\/y_white_64.png","findhidden\/q_white_64.png","findhidden\/l_white_64.png","findhidden\/t_white_64.png","findhidden\/h_white_64.png","findhidden\/v_white_64.png","findhidden\/r_white_64.png","findhidden\/k_white_64.png","findhidden\/m_white_64.png","findhidden\/c_white_64.png","findhidden\/z_white_64.png","findhidden\/s_white_64.png","findhidden\/w_white_64.png","findhidden\/d_white_64.png","findhidden\/i_white_64.png","findhidden\/e_white_64.png"]
07-03 19:16:37.310: D/temp1(8863): ["findhidden\/o_64.png","findhidden\/x_64.png","findhidden\/f_64.png","findhidden\/b_64.png","findhidden\/a_64.png","findhidden\/g_64.png","findhidden\/p_64.png","findhidden\/u_64.png","findhidden\/n_64.png","findhidden\/j_64.png","findhidden\/y_64.png","findhidden\/q_64.png","findhidden\/l_64.png","findhidden\/t_64.png","findhidden\/h_64.png","findhidden\/v_64.png","findhidden\/r_64.png","findhidden\/k_64.png","findhidden\/m_64.png","findhidden\/c_64.png","findhidden\/z_64.png","findhidden\/s_64.png","findhidden\/w_64.png","findhidden\/d_64.png","findhidden\/i_64.png","findhidden\/e_64.png"]
07-03 19:16:37.310: D/temp2(8863): {"arr":"[\"findhidden\\\/o_64.png\",\"findhidden\\\/x_64.png\",\"findhidden\\\/f_64.png\",\"findhidden\\\/b_64.png\",\"findhidden\\\/a_64.png\",\"findhidden\\\/g_64.png\",\"findhidden\\\/p_64.png\",\"findhidden\\\/u_64.png\",\"findhidden\\\/n_64.png\",\"findhidden\\\/j_64.png\",\"findhidden\\\/y_64.png\",\"findhidden\\\/q_64.png\",\"findhidden\\\/l_64.png\",\"findhidden\\\/t_64.png\",\"findhidden\\\/h_64.png\",\"findhidden\\\/v_64.png\",\"findhidden\\\/r_64.png\",\"findhidden\\\/k_64.png\",\"findhidden\\\/m_64.png\",\"findhidden\\\/c_64.png\",\"findhidden\\\/z_64.png\",\"findhidden\\\/s_64.png\",\"findhidden\\\/w_64.png\",\"findhidden\\\/d_64.png\",\"findhidden\\\/i_64.png\",\"findhidden\\\/e_64.png\"]"}

更多LOGCAT

07-03 19:16:37.320: W/System.err(8863):     at org.json.JSON.typeMismatch(JSON.java:96)

2 个答案:

答案 0 :(得分:2)

从temp2的日志输出中,arr的json类型是String。因此,如果您尝试从中获取JSONArray,则会获得typeMismatch。

查看以下代码是否适合您。 JSONObject的构造函数可以采用包含对象的JSON编码字符串。所以我们用它来创建temp2。

Log.d("defaultArrayShuffled", defaultArray.toString());

String temp = defaultArray.toString();
String temp1= temp.replaceAll("_white_", "_");
Log.d("temp1", temp1);

JSONObject temp2 = new JSONObject("{ \"arr\" : " + temp1 + "}");
//temp2.put("arr", temp1);
Log.d("temp2", temp2.toString());
replacingArray = temp2.getJSONArray("arr");

希望它有所帮助。

答案 1 :(得分:0)

几个想法 -
答:我发现杰克逊是一个更好的Json图书馆。考虑使用它。它提供了更好的错误消息 B.我编写了以下代码,类似于您使用我在www.ovirt.org处为Json序列化和反序列化所做的infra所做的事情:

 String[] arr = { "aaa_white_.png", "bbb_white_.png", "ccc_white_.png", "ddd.png" };
        JsonObjectSerializer serialezer = new JsonObjectSerializer();
        String payload = serialezer.serialize(arr);
        String newPayload = payload.replaceAll("_white_", "_");
        JsonObjectDeserializer der = new JsonObjectDeserializer();
        String[] res = (String[]) der.deserialize(newPayload, String[].class);
        System.out.println(payload);
        System.out.println(res);
        for (String str : res) {
            System.out.println(str);
        }

你可以克隆我们的代码,你应该看看ovirt-engine/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/serialization/json 看看我们在那里做了什么。