我如何解析类似于JSON的字符串?

时间:2019-11-26 14:00:29

标签: java json parsing

在我的项目中,我正在向Stripe提交一个请求,该请求返回文件上的一系列卡片

{
data = [{
    address_zip_check = null,
    country = US,
    last4 = 4242,
    funding = credit,
    metadata = {},
    address_country = null,
    address_state = null,
    exp_month = 4,
    exp_year = 2024,
    address_city = null,
    tokenization_method = null,
    cvc_check = pass,
    address_line2 = null,
    address_line1 = null,
    fingerprint = TAw8rdASXn64,
    name = null,
    id = card_1FiwAkASrE1bJO6aaQ,
    address_line1_check = null,
    address_zip = null,
    dynamic_last4 = null,
    brand = Visa,
    object = card,
    customer = cus_GDUhOvTHo9
}, {
    address_zip_check = null,
    country = US,
    last4 = 4444,
    funding = credit,
    metadata = {},
    address_country = null,
    address_state = null,
    exp_month = 4,
    exp_year = 2024,
    address_city = null,
    tokenization_method = null,
    cvc_check = pass,
    address_line2 = null,
    address_line1 = null,
    fingerprint = NW3wMUNiaA8g,
    name = null,
    id = card_1Fj3ZoASrE1bJO6aFu,
    address_line1_check = null,
    address_zip = null,
    dynamic_last4 = null,
    brand = MasterCard,
    object = card,
    customer = cus_GDUhOvTHo9
}], has_more = false, url = /v1/customers / cus_GDUhOvTHo9 / sources, object = list

}

问题是JSONObjectJSONArray无法将其识别为有效的JSON。

JSONObject jsonObject = new JSONObject(jsonStr);
JSONArray jsonArray = jsonObject.getJSONArray("data");

"Expected literal value..."

我如何解析此信息?

是否可以修改此字符串,以便将其识别为有效的JSON?

我必须编写自己的解析方法吗?

1 个答案:

答案 0 :(得分:3)

  

返回文件

上的一系列卡片
  1. Stripe-API返回有效的json [1]。

  2. 我猜想故障发生在例如当您将响应序列化为文件时。确保使用正确的json工具!

  3. 您的印刷品看起来像普通的Map.toString()。只需将地图直接传递到new JSONObject(mymap)

[1] curl https://api.stripe.com/v1/customers -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: