3

我有这个 JSON 对象:

{"error":null,
 "result":[{"id":"1234567890",
            "count":1,
            "recipients":
            ["u3848",
               "u8958",
               "u7477474"
            ],
            "dateCreated":"2012-06-13T09:13:45.989Z"
           }]
 }

我正在尝试找到一种将recipients数组正确解析为String[]对象的方法。

是否有捷径可寻?

编辑:

找到了包含结果所需的所有内容的答案:Sending and Parsing JSON Objects

4

3 回答 3

13

做我想做的事情是这样的:

JSONArray temp = jsonObject.getJSONArray("name");
int length = temp.length();
if (length > 0) {
    String [] recipients = new String [length];
    for (int i = 0; i < length; i++) {
        recipients[i] = temp.getString(i);
    }
}
于 2012-06-26T07:19:30.520 回答
0

I always suggest my favorite library json-lib to handle JSON stuffs.

You can use JSONArray to convert to Object[], although it's not String[], you can still use it because every Object has toString() method.

String sYourJsonString = "['u3848', 'u8958', 'u7477474']";
Object[] arrayReceipients = JSONArray.toArray (JSONArray.fromObject(sYourJsonString));
System.out.println (arrayReceipients [0]); // u3848
于 2012-06-13T10:03:46.667 回答
0

您可以尝试将 xstream 与 json 序列化程序一起使用。看看这个链接

于 2012-06-13T09:44:26.683 回答