當前位置: 首頁>>代碼示例>>Java>>正文


Java JSON.toJSONString方法代碼示例

本文整理匯總了Java中com.alibaba.fastjson.JSON.toJSONString方法的典型用法代碼示例。如果您正苦於以下問題:Java JSON.toJSONString方法的具體用法?Java JSON.toJSONString怎麽用?Java JSON.toJSONString使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.alibaba.fastjson.JSON的用法示例。


在下文中一共展示了JSON.toJSONString方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: test_1

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
public void test_1() throws Exception {
    Model[] array = new Model[2048];
    for (int i = 0; i < array.length; ++i) {
        array[i] = new Model();
        array[i].value = Type.A;
    }

    String text = JSON.toJSONString(array);

    Model[] array2 = JSON.parseObject(text, Model[].class);

    Assert.assertEquals(array.length, array2.length);
    for (int i = 0; i < array.length; ++i) {
        Assert.assertEquals(array[i].value, array2[i].value);
    }
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:17,代碼來源:EnumFieldTest3_private.java

示例2: test_0

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
public void test_0() throws Exception {
    Model model = new Model();

    String text = JSON.toJSONString(model, SerializerFeature.BeanToArray);
    Assert.assertEquals("[null]", text);

    Model model2 = JSON.parseObject(text, Model.class, Feature.SupportArrayToBean);
    Assert.assertNull(model2.name);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:10,代碼來源:WriteAsArray_string.java

示例3: test_0

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
public void test_0() throws Exception {
    VO vo = new VO();
    vo.id = 100;
    
    String text = JSON.toJSONString(vo, SerializerFeature.WriteNonStringValueAsString);
     Assert.assertEquals("{\"id\":\"100\"}", text);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:8,代碼來源:WriteNonStringValueAsStringTestByteField.java

示例4: test_codec_null_2

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
public void test_codec_null_2() throws Exception {
    V0 v = JSON.parseObject("{\"value\":[1,2]}", V0.class);

    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(false);

    String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty);
    Assert.assertEquals("{\"value\":[1,2]}", text);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:10,代碼來源:AtomicLongArrayFieldTest.java

示例5: test_1

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
public void test_1() throws Exception {
    SerializeConfig config = new SerializeConfig();
    config.setAsmEnable(true);
    
    String text = JSON.toJSONString(new Entity(), config);
    Assert.assertEquals("{\"value\":\"\"}", text);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:8,代碼來源:FeaturesTest4.java

示例6: test_create

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
public void test_create() throws Exception {
    Entity entity = new Entity(123.45D, "菜姐");
    String text = JSON.toJSONString(entity);
    
    Entity entity2 = JSON.parseObject(text, Entity.class);
    Assert.assertTrue(entity.getId() == entity2.getId());
    Assert.assertEquals(entity.getName(), entity2.getName());
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:9,代碼來源:JSONCreatorTest_double.java

示例7: run

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
@Override
public void run() {
    LOG.info("MessageProcesserThread process message start...");
    try {
        byte [] body=message.getBody();
        if(null==body){
            return;
        }
        String bodyStr = new String(body,"UTF-8");

        //消息數據轉化為ElvesMqMessage,驗證數據結構是否符合標準
        ElvesMqMessage elvesMsg = ElvesMqMessage.getInstance(bodyStr);
        if(null==elvesMsg){
            LOG.error("Message structure error!");
            return;
        }

        //elves consumer 服務類進行消費消息
        Object serviceBean=SpringUtil.getBean(this.SERVICE_NAME);
        Class clazz = serviceBean.getClass();
        Method method = clazz.getDeclaredMethod(elvesMsg.getAction(),Map.class);
        Object result = method.invoke(serviceBean,elvesMsg.getMqbody());

        //消息處理結果為null ,或者cast類型的消息,後續不做處理
        if(result==null){
            return;
        }
        Map<String,Object> back =new HashMap<String,Object>();
        back.put("mqkey",elvesMsg.getToModule()+"."+elvesMsg.getFromModule());
        back.put("mqflag","cast");
        back.put("mqbody",result);
        String responseMsg =JSON.toJSONString(back);
        LOG.info("Response msg :"+responseMsg);
        messageProducer.reply(message.getMessageProperties().getReplyTo(),responseMsg);
    } catch (Exception e) {
        LOG.error(ExceptionUtil.getStackTraceAsString(e));
    }
    LOG.info("MessageProcesserThread process message end.");
}
 
開發者ID:elves-project,項目名稱:scheduler,代碼行數:40,代碼來源:MessageProcesserThread.java

示例8: test_ref

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
public void test_ref() throws Exception {
    Department tech = new Department(1, "技術部");
    tech.setRoot(tech);
    
    {
        Department pt = new Department(2, "平台技術部");
        pt.setParent(tech);
        pt.setRoot(tech);
        tech.getChildren().add(pt);
        {
            Department sysbase = new Department(3, "係統基礎");
            sysbase.setParent(pt);
            sysbase.setRoot(tech);
            pt.getChildren().add(sysbase);
        }
    }
    {
        Department cn = new Department(4, "中文站技術部");
        cn.setParent(tech);
        cn.setRoot(tech);
        tech.getChildren().add(cn);
    }
    
    {
        //JSON.toJSONString(tech);
    }
    
    {
        String prettyText = JSON.toJSONString(tech, SerializerFeature.PrettyFormat);
        System.out.println(prettyText);
    
        String text = JSON.toJSONString(tech);
        Department dept = JSON.parseObject(text, Department.class);
        Assert.assertTrue(dept == dept.getRoot());
        
        System.out.println(JSON.toJSONString(dept, SerializerFeature.PrettyFormat));
    }
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:39,代碼來源:RefTest11.java

示例9: test_color

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
public void test_color() throws Exception {
    JSONSerializer serializer = new JSONSerializer();
    Assert.assertEquals(AwtCodec.class, serializer.getObjectWriter(Point.class).getClass());
    
    Point point = new Point(3, 4);
    String text = JSON.toJSONString(point);

    Point point2 = JSON.parseObject(text, Point.class);

    Assert.assertEquals(point, point2);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:12,代碼來源:PointTest.java

示例10: test_for_mtop

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
public void test_for_mtop() throws Exception {
    P0 p = new P0();
    p.model = new Model();

    ContextValueFilter valueFilter = new ContextValueFilter() {

        @Override
        public Object process(BeanContext context, Object object, String name, Object value) {


            if (value instanceof Model) {
                Assert.assertEquals(P0.class, context.getBeanClass());
                Assert.assertNotNull(context.getField());
                Assert.assertNotNull(context.getMethod());
                Assert.assertEquals("model", context.getName());
                Assert.assertEquals(Model.class, context.getFieldClass());
                Assert.assertEquals(Model.class, context.getFieldType());
                Assert.assertEquals(SerializerFeature.WriteMapNullValue.mask, context.getFeatures());
                
                Field field = context.getField();
                Assert.assertNotNull(field.getAnnotation(UrlIdentify.class));
                Assert.assertNotNull(context.getAnnation(UrlIdentify.class));
                
                return value;
            }

            return value;
        }
    };

    JSON.toJSONString(p, valueFilter);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:33,代碼來源:MTopTest.java

示例11: test_format

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
public void test_format() throws Exception {
    Model model = new Model();
    model.value = 123.45678D;

    String str = JSON.toJSONString(model);
    assertEquals("{\"value\":123.46}", str);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:8,代碼來源:DoubleFormatTest2.java

示例12: test_for_issue

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
public void test_for_issue() throws Exception {
    LocalDateTime dateTime = LocalDateTime.of(2016, 5, 6, 9, 3, 16);
    VO vo = new VO();
    vo.setDate(dateTime);
    
    String text = JSON.toJSONString(vo);
    Assert.assertEquals("{\"date\":\"2016-05-06 09:03:16\"}", text);
    
    VO vo1 = JSON.parseObject(text, VO.class);
    
    Assert.assertEquals(vo.getDate(), vo1.getDate());
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:13,代碼來源:LocalDateTimeTest4.java

示例13: test_empty

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
public void test_empty() throws Exception {
    Health vo = new Health();
    vo.id = 123;
    vo.border = 234;

    String text = JSON.toJSONString(vo);
    Assert.assertEquals("{\"border\":234,\"id\":123}", text);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:9,代碼來源:JSONFieldTest_unwrapped_4.java

示例14: test_array

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
public void test_array() throws Exception {
    List<Entity> list = new ArrayList<Entity>();
    list.add(new Entity(123, "aaa"));
    list.add(new Entity(234, "bbb"));
    list.add(new Entity(3, "ccc"));
    String text = JSON.toJSONString(list, SerializerFeature.PrettyFormat, SerializerFeature.UseSingleQuotes);
    Assert.assertEquals("[\n\t{\n\t\t'id':123,\n\t\t'name':'aaa'\n\t},\n\t{\n\t\t'id':234,\n\t\t'name':'bbb'\n\t},\n\t{\n\t\t'id':3,\n\t\t'name':'ccc'\n\t}\n]", text);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:9,代碼來源:ArrayListTest.java

示例15: test_unicode

import com.alibaba.fastjson.JSON; //導入方法依賴的package包/類
public void test_unicode() throws Exception {
    String text = JSON.toJSONString(Collections.singletonMap("v", "\u0018"));
    System.out.println(text);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:5,代碼來源:UnicodeTest.java


注:本文中的com.alibaba.fastjson.JSON.toJSONString方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。