0

我的 JSON 字符串如下所示:

"{\"TrackingInformationResponse\": {\u000a  \"shipments\": [\u000a    {\u000a      \"shipmentId\": \"03015035146308\",\u000a      \"uri\": \"\/ntt-service-rest\/api\/shipment\/03015035146308\/0\",\u000a      \"assessedNumberOfItems\": 1,\u000a      \"deliveryDate\": \"2013-05-13T11:47:00\",\u000a      \"estimatedTimeOfArrival\": \"2013-05-13T16:00:00\",\u000a      \"service\": {\u000a        \"code\": \"88\",\u000a        \"name\": \"DPD\"\u000a      },\u000a      \"consignor\": {\u000a        \"name\": \"Webhallen Danmark ApS\",\u000a        \"address\": {\u000a          \"street1\": \"Elsa Brändströms Gata 52\",\u000a          \"city\": \"HÄGERSTEN\",\u000a          \"countryCode\": \"SWE\",\u000a          \"country\": \"Sverige\",\u000a          \"postCode\": \"12952\"\u000a        }\u000a      },\u000a      \"consignee\": {\u000a        \"name\": \"Lene Bjerre Kontor & IT Service\",\u000a        \"address\": {\u000a          \"street1\": \"Lene Bjerre\",\u000a          \"street2\": \"Ørbækvej 8, Hoven\",\u000a          \"city\": \"TARM\",\u000a          \"countryCode\": \"???\",\u000a          \"postCode\": \"6880\"\u000a        }\u000a      },\u000a      \"statusText\": {\u000a        \"header\": \"Forsendelsen er udleveret\",\u000a        \"body\": \"Forsendelsen blev leveret 13-05-2013 kl. 11:47\"\u000a      },\u000a      \"status\": \"DELIVERED\",\u000a      \"totalWeight\": {\u000a        \"value\": \"0.55\",\u000a        \"unit\": \"kg\"\u000a      },\u000a      \"totalVolume\": {\u000a        \"value\": \"0.005\",\u000a        \"unit\": \"m3\"\u000a      },\u000a      \"items\": [\u000a        {\u000a          \"itemId\": \"03015035146308\",\u000a          \"dropOffDate\": \"2013-05-08T17:18:00\",\u000a          \"deliveryDate\": \"2013-05-13T11:47:00\",\u000a          \"status\": \"DELIVERED\",\u000a          \"statusText\": {\u000a            \"header\": \"Forsendelsen er udleveret til modtageren\",\u000a            \"body\": \"Forsendelsen blev udleveret  13-05-2013 kl. 11:47\"\u000a          },\u000a          \"statedMeasurement\": {\u000a            \"weight\": {\u000a              \"value\": \"0.55\",\u000a              \"unit\": \"kg\"\u000a            },\u000a            \"length\": {\u000a              \"value\": \"0.28\",\u000a              \"unit\": \"m\"\u000a            },\u000a            \"height\": {\u000a              \"value\": \"0.1\",\u000a              \"unit\": \"m\"\u000a            },\u000a            \"width\": {\u000a              \"value\": \"0.2\",\u000a              \"unit\": \"m\"\u000a            },\u000a            \"volume\": {\u000a              \"value\": \"0.005\",\u000a              \"unit\": \"m3\"\u000a            }\u000a          },\u000a          \"events\": [\u000a            {\u000a              \"eventTime\": \"2013-05-08T16:42:00\",\u000a              \"eventCode\": \"68\",\u000a              \"eventDescription\": \"Oplysning om forsendelsen er modtaget\",\u000a              \"location\": {\u000a                \"displayName\": \"Webhallen Danmark ApS\",\u000a                \"name\": \"Webhallen Danmark ApS\",\u000a                \"countryCode\": \"SWE\",\u000a                \"country\": \"Sverige\"\u000a              }\u000a            },\u000a            {\u000a              \"eventTime\": \"2013-05-08T17:18:00\",\u000a              \"eventCode\": \"74\",\u000a              \"eventDescription\": \"Transporten af forsendelsen er påbegyndt\",\u000a              \"location\": {\u000a                \"locationId\": \"126\",\u000a                \"displayName\": \"Segeltorp\",\u000a                \"name\": \"Segeltorp\",\u000a                \"countryCode\": \"SWE\",\u000a                \"country\": \"Sverige\",\u000a                \"postcode\": \"14172\",\u000a                \"city\": \"Segeltorp\",\u000a                \"locationType\": \"HUB\"\u000a              }\u000a            },\u000a            {\u000a              \"eventTime\": \"2013-05-08T17:18:00\",\u000a              \"eventCode\": \"31\",\u000a              \"eventDescription\": \"Forsendelsen er på vej\",\u000a              \"location\": {\u000a                \"locationId\": \"126\",\u000a                \"displayName\": \"Segeltorp\",\u000a                \"name\": \"Segeltorp\",\u000a                \"countryCode\": \"SWE\",\u000a                \"country\": \"Sverige\",\u000a                \"postcode\": \"14172\",\u000a                \"city\": \"Segeltorp\",\u000a                \"locationType\": \"HUB\"\u000a              }\u000a            },\u000a            {\u000a              \"eventTime\": \"2013-05-10T04:34:00\",\u000a              \"eventCode\": \"31\",\u000a              \"eventDescription\": \"Forsendelsen er på vej\",\u000a              \"location\": {\u000a                \"locationId\": \"201\",\u000a                \"displayName\": \"Malmö\",\u000a                \"name\": \"Malmö\",\u000a                \"countryCode\": \"SWE\",\u000a                \"country\": \"Sverige\",\u000a                \"postcode\": \"21241\",\u000a                \"city\": \"Malmö\",\u000a                \"locationType\": \"HUB\"\u000a              }\u000a            },\u000a            {\u000a              \"eventTime\": \"2013-05-10T22:46:13\",\u000a              \"eventCode\": \"31\",\u000a              \"eventDescription\": \"Forsendelsen er på vej\",\u000a              \"location\": {\u000a                \"locationId\": \"0707\",\u000a                \"displayName\": \"Kolding Pakkecenter\",\u000a                \"name\": \"Kolding Pakkecenter\",\u000a                \"countryCode\": \"DNK\",\u000a                \"country\": \"Danmark\",\u000a                \"postcode\": \"2605\",\u000a                \"city\": \"Brøndby\",\u000a                \"locationType\": \"HUB\"\u000a              }\u000a            },\u000a            {\u000a              \"eventTime\": \"2013-05-11T01:05:00\",\u000a              \"eventCode\": \"31\",\u000a              \"eventDescription\": \"Forsendelsen er på vej\",\u000a              \"location\": {\u000a                \"locationId\": \"0050\",\u000a                \"displayName\": \"Kolding\",\u000a                \"name\": \"Kolding\",\u000a                \"countryCode\": \"DNK\",\u000a                \"country\": \"Danmark\",\u000a                \"locationType\": \"DPD_DEPOT\"\u000a              }\u000a            },\u000a            {\u000a              \"eventTime\": \"2013-05-13T07:59:00\",\u000a              \"eventCode\": \"113\",\u000a              \"eventDescription\": \"Forsendelsen gøres klar til levering\",\u000a              \"location\": {\u000a                \"locationId\": \"0506\",\u000a                \"displayName\": \"Herning\",\u000a                \"name\": \"Herning\",\u000a                \"countryCode\": \"DNK\",\u000a                \"country\": \"Danmark\",\u000a                \"locationType\": \"DPD_DEPOT\"\u000a              }\u000a            },\u000a            {\u000a              \"eventTime\": \"2013-05-13T07:59:00\",\u000a              \"eventCode\": \"31\",\u000a              \"eventDescription\": \"Forsendelsen er på vej\",\u000a              \"location\": {\u000a                \"locationId\": \"0506\",\u000a                \"displayName\": \"Herning\",\u000a                \"name\": \"Herning\",\u000a                \"countryCode\": \"DNK\",\u000a                \"country\": \"Danmark\",\u000a                \"locationType\": \"DPD_DEPOT\"\u000a              }\u000a            },\u000a            {\u000a              \"eventTime\": \"2013-05-13T07:59:44\",\u000a              \"eventCode\": \"z65\",\u000a              \"eventDescription\": \"Forsendelsen er ankommet distributionscenter\",\u000a              \"location\": {\u000a                \"locationId\": \"6900\",\u000a                \"displayName\": \"Skjern\",\u000a                \"name\": \"Skjern Distributionscenter\",\u000a                \"countryCode\": \"DNK\",\u000a                \"country\": \"Danmark\",\u000a                \"postcode\": \"6900\",\u000a                \"city\": \"Skjern\",\u000a                \"locationType\": \"DEPOT\"\u000a              }\u000a            },\u000a            {\u000a              \"eventTime\": \"2013-05-13T11:47:00\",\u000a              \"eventCode\": \"21\",\u000a              \"eventDescription\": \"Forsendelsen er udleveret\",\u000a              \"location\": {\u000a                \"locationId\": \"0506\",\u000a                \"displayName\": \"Herning\",\u000a                \"name\": \"Herning\",\u000a                \"countryCode\": \"DNK\",\u000a                \"country\": \"Danmark\",\u000a                \"locationType\": \"DPD_DEPOT\"\u000a              }\u000a            }\u000a          ],\u000a          \"references\": [\u000a            {\u000a              \"value\": \"5008074\",\u000a              \"type\": \"CU\",\u000a              \"name\": \"Afsenderreference \"\u000a            }\u000a          ]\u000a        }\u000a      ],\u000a      \"additionalServices\": [\u000a      ],\u000a      \"splitStatuses\": [\u000a      ],\u000a      \"shipmentReferences\": [\u000a        {\u000a          \"value\": \"5008074\",\u000a          \"type\": \"CU\",\u000a          \"name\": \"Afsenderreference \"\u000a        }\u000a      ]\u000a    }\u000a  ]\u000a}}"

我使用以下代码从服务器获取这些数据:

    public String readJSONFeed(String URL) {
    StringBuilder stringBuilder = new StringBuilder();
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(URL);
    try {
        HttpResponse response = httpClient.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream inputStream = entity.getContent();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(inputStream, "UTF-8"));
            String line;
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
            }
            inputStream.close();
        } else {
            Log.d("JSON", "Failed to download file");
        }
    } catch (Exception e) {
        Log.d("readJSONFeed", e.getLocalizedMessage());
    }        
    return stringBuilder.toString();
}

当我尝试解析字符串时

JSONObject jsonObject = new JSONObject(result);

,我得到一个异常,很可能是因为 JSON 无效。如果我尝试像这样手动替换 unicode 字符,它会完美运行:

  • \u000a 替换为新行
  • \" 仅替换为 "
  • 第一个和最后一个 " 被删除

我将如何解析这些数据?

4

1 回答 1

0
"{\"TrackingInformationResponse\": {\u000a  \"shipments\": [\u000a...

您有一个 JSON 字符串,它本身以字符串文字格式包装。不是 100% 确定包装字符串格式是什么,因为有许多格式使用\u\"转义 - 您应该在您调用的服务上获取任何文档,以尝试弄清楚它应该是什么。将 Web 服务返回结果包装成这样的字符串文字格式似乎很奇怪,也没有必要。

一种可能性是 Java 字符串文字;在这种情况下,请参阅Apache 的 StringEscapeUtils 中的unescapeJava

然而,最有可能的是 JSON 字符串文字 - 即双 JSON 编码。在这种情况下,您必须先对字符串进行一次 JSON 解码,然后再查看其中的嵌套 JSON 结构。

但是,假设您使用的是 org.json,则没有直接的JSONString解析器可以与JSONObjectandJSONArray解析器一起使用。但是,您始终可以将字符串包装在其中一个可以理解的结构中,例如:

String unwrapped= new JSONArray("["+result+"]").get(0);
JSONObject o= new JSONObject(unwrapped);
于 2013-05-28T14:46:21.023 回答