0

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

代码

                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\"]"}

更多

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

2 回答 2

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

希望能帮助到你。

于 2012-07-03T14:36:00.013 回答
0

几个想法 -
A. 我发现 Jackson 是一个更好的 Json 库。考虑使用它。它提供了更好的错误消息
B. 我编写了以下代码,类似于您使用我们在www.ovirt.org 上为 Json 序列化和反序列化所做的基础设施所做的事情:

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

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

于 2012-07-03T14:23:55.607 回答