解决方法:
Python:
import json
api_response = '{"message": "This is \\"escaped\\"."}'
parsed_response = json.loads(api_response)
print(parsed_response['message']) # Output: This is "escaped".
Java:
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) throws JsonProcessingException {
String apiResponse = "{\"message\": \"This is \\\"escaped\\\".\"}";
ObjectMapper objectMapper = new ObjectMapper();
ApiResponse parsedResponse = objectMapper.readValue(apiResponse, ApiResponse.class);
System.out.println(parsedResponse.getMessage()); // Output: This is "escaped".
}
}
class ApiResponse {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Python:
import json
api_response = '{"message": "This is \\"escaped\\"."}'
parsed_response = json.loads(api_response)
print(parsed_response['message']) # Output: This is "escaped".
Java:
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) throws JsonProcessingException {
String apiResponse = "{\"message\": \"This is \\\"escaped\\\".\"}";
ObjectMapper objectMapper = new ObjectMapper();
JsonNode parsedResponse = objectMapper.readTree(apiResponse);
System.out.println(parsedResponse.get("message").asText()); // Output: This is "escaped".
}
}
请注意,以上示例代码仅作为参考,具体的解决方法可能因编程语言、使用的库和API响应的格式而有所不同。根据实际情况进行调整。
下一篇:API响应被多次接收