检查字符串是否包含xml或json数据 [英] Check if string either contains xml or json data
问题描述
我收到一个包含xml或json内容的字符串.
I receive a String which contains either xml or json contents.
如果String包含json内容,则使用jackson(java api)将 JSON转换为Java对象
If String contains json content I use jackson(java api) to Convert JSON to Java object
如果其中包含xml内容,我将使用JAXB将XML内容转换为Java对象(解组).
And if it contains xml contents I use JAXB to Convert XML content into a Java Object(Unmarshalling).
如何检查该字符串中是否接收到xml或json?
How can I check whether I receive xml or json in that string?
推荐答案
XML文档实体(通常来说是XML文档)必须以<"开头.
An XML document entity (in common parlance, an XML document) must start with "<".
根据ECMA-404,JSON文本以零个或多个空格字符开头,后跟以下任一字符:
A JSON text, according to ECMA-404, starts with zero or more whitespace characters followed by one of the following:
{ [ " 0-9 - true false null
因此,最简单的方法就是测试if(s.startsWith("<")
So your simplest approach is just to test if(s.startsWith("<")
这篇关于检查字符串是否包含xml或json数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!