记录几个Json的lib

1, jettison

jettison可以转换json和xml格式,并通过这种途径来将json字符串解析成Java对象。其采用STAX的方式进行json转换,用法如下

public class Parse

{

	/**

	 * @param args

	 * @throws JSONException 

	 */

	public static void main(String[] args) throws JSONException

	{

		JSONObject userString = new JSONObject("{\"jaxb\":{\"name\" : \"abc\"}}");



		AbstractXMLStreamReader reader = null;

        try

        {

	        reader = new MappedXMLStreamReader(userString);

	        

	        try

            {

	            JAXBContext jc = JAXBContext.newInstance(Jaxb.class);

	            

	            Unmarshaller unmarshaller = jc.createUnmarshaller();

	            

	            Object jaxb = unmarshaller.unmarshal(reader);

	            

	            System.out.println(jaxb);

	            

	            StringWriter sw = new StringWriter();

	            MappedNamespaceConvention con = new MappedNamespaceConvention(new Configuration());

	            XMLStreamWriter xsw = new MappedXMLStreamWriter(con, sw);

	            

	            Marshaller marshaller = jc.createMarshaller();

	            marshaller.marshal(jaxb, xsw);

	            

	            System.out.println(sw.toString());

	            try

                {

	                sw.close();

                } catch (IOException e)

                {

	                e.printStackTrace();

                }

	            

            } catch (JAXBException e)

            {

	            e.printStackTrace();

            }

        } catch (XMLStreamException e1)

        {

	        e1.printStackTrace();

        }

		

        if (reader != null)

        {

			try

            {

                reader.close();

            } catch (XMLStreamException e)

            {

                e.printStackTrace();

            }

        }

	}



}

 

2,jackson

jackson可以提供灵活的转换策略,例如是否在转换中将根属性识别为对象类型(jettison会,json-lib不会,jackson默认不会,根据选项判断),在遇到不能识别的属性时是否抛出异常(jettison不会,json-lib不会,jackson默认会,可根据选项判断)等

public class DataBindingParser

{



	/**

	 * @param args

	 */

	@SuppressWarnings("unchecked")

    public static void main(String[] args)

	{

		String userString = "{\"User\":{\"name\" : { \"first\" : \"Joe\", \"last\" : \"Sixpack\" },\"gender\" : \"MALE\", \"verified\" : false, \"userImage\" : \"Rm9vYmFyIQ==\"}}";

		String userStringEx = "{\"name\" : { \"first\" : \"Joe\", \"last\" : \"Sixpack\" },\"gender\" : \"MALE\", \"age\" : 18, \"verified\" : false, \"userImage\" : \"Rm9vYmFyIQ==\"}";

		

		ObjectMapper mapper = new ObjectMapper(); 

		

		try

		{

			mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);

			mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);

			User user = mapper.readValue(userString, User.class);

			System.out.println("userString-->" + user);

			System.out.println("userString<--" + mapper.writeValueAsString(user));

			

			mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

			mapper.disable(DeserializationFeature.UNWRAP_ROOT_VALUE);

			User userEx = mapper.readValue(userStringEx, User.class);

			System.out.println("userStringEx-->" + userEx);

			System.out.println("userStringEx<--" + mapper.writeValueAsString(userEx));

			System.out.println("=========================");

			

            Map<String,Object> userData = mapper.readValue(userString, Map.class);

			System.out.println("userString-->" + userData);

			System.out.println("userString<--" + mapper.writeValueAsString(userData));

			

            Map<String,Object> userDataEx = mapper.readValue(userStringEx, Map.class);

			System.out.println("userStringEx-->" + userDataEx);

			System.out.println("userStringEx<--" + mapper.writeValueAsString(userDataEx));

			

		} catch (JsonParseException e)

		{

			e.printStackTrace();

		} catch (JsonMappingException e)

		{

			e.printStackTrace();

		} catch (IOException e)

		{

			e.printStackTrace();

		}

	}



}

  

 

你可能感兴趣的:(json)