0

我有一个适用于 java 的谷歌应用引擎应用程序,它有一个 facebook 表单,在提交时会向我们应用程序中的 servlet 发送一个签名请求。我们正在使用以下代码解密并转换为 json 字符串

String signedRequest = (String) req.getParameter("signed_request");
        String payload = signedRequest.split("[.]", 2)[1];
        payload = payload.replace("-", "+").replace("_", "/").trim();
        String jsonString = new String(Base64.decodeBase64(payload.getBytes()));
        System.out.println("Json is::" + jsonString);

响应看起来像

[sakshumweb/3.361739372881481188].: Json 是::{"算法":"HMAC-SHA256","expires":1347588000,"issued_at":1347584290,"oauth_token":"XXXXX","re​​gistration":{"name" :"Vik Ku​​mar","first_name":"Vik","last_name":"Kumar","bloodGroup":"B-","gender":"male","birthday":"10/31/1983" ,"email":"vik.ceo\u0040gmail.com","cellPhone":"1234123456","homePhone":"1234123457","officePhone":"1234123458","primaryAddress":"jdfjfgj","area" :"jfdjdfj","location":{"name":"加州红木海岸","id":103107903062719},"subscribe":true,"eyePledge":false,"reference":"fgfgfgfg"}, "registration_metadata":{"fields":"[{\"name\":\"name\"},{\"name\ ":\"first_name\"},{\"name\":\"last_name\"}, {\"name\":\"bloodGroup\", \"description\":\"Blood Group\", \ "类型\":\"选择\",\"选项\":{\"A+\":\"A+\",\"A-\":\"A-\",\"B+\": \"B+\",\"B-\":\"B-\",\"O+\":\"O+\",\"O-\":\"O-\",\"AB+\ ":\"AB+\",\"AB-\":\"AB-\",\"A1+\":\"A1+\",\"A1-\":\"A1-\",\" A2+\":\"A2+\",\"A2-\":\"A2-\",\"A1B+\":\"A1B+\",\"A1B-\":\"A1B-\",\"A2B+\":\"A2B+\",\"A2B-\": \"A2B-\",\"HH\":\"孟买血型\"}}, {\"姓名\":\"性别\"}, {\"姓名\":\"生日\"} ,{\"name\":\"email\"}, {\"name\":\"cellPhone\", \"description\":\"Cell Number\", \"type\":\"text \"}, {\"name\":\"homePhone\", \"description\":\"Home Number\", \"type\":\"text\"}, {\"name\": \"officePhone\", \"description\":\"Office Number\", \"type\":\"text\"}, {\"name\":\"primaryAddress\", \"description\" :\"主要地址\", \"type\":\"text\"}, {\"name\":\"area\", \"description\":\"Locality/Village/Area\", \"type \":\"text\"},{\"name\":\"location\"}, {\"name\":\"subscribe\", \"description\":\"订阅我的 Sakshum活动更新。\", \"type\":\"checkbox\", \"default\":\"checked\"}, {\"name\":\"eyePledge\", \"description\": \"我也想发誓。\", \"type\":\"checkbox\"}, {\"name\":\"reference\", \"description\":\"How you联系我们(朋友、Facebook、谷歌等)?\", \"type\":\"text\"}]"},"user":{"国家":"us","locale":"en_GB"},"user_id":"875390603"}

那么,我如何解析这些数据以提取此响应的注册部分中的数据?

4

1 回答 1

1

由于这是一个有效的 JSON,您可以使用 JSON 库(如 Jackson 或GS​​ON)来解析它。

您可以使用此示例代码打印出所有registration字段:

JsonNode json = new ObjectMapper().readTree(response);
JsonNode registration_fields = json.get("registration");

Iterator<String> fieldNames = registration_fields.getFieldNames();
while(fieldNames.hasNext()){
    String fieldName = fieldNames.next();
    String fieldValue = registration_fields.get(fieldName).asText();
    System.out.println(fieldName+" : "+fieldValue);
}
于 2012-09-14T06:49:39.840 回答