如何在Java中生成随机JSON字符串?

编程入门 行业动态 更新时间:2024-10-18 20:19:23
本文介绍了如何在Java中生成随机JSON字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想测试apache kafka的功能.因此,我需要用Java生成随机JSON字符串.(我的生产数据是JSON格式.)

I want to test a feature of apache kafka.So I need generate random JSON string in Java.(My production data is JSON format.)

我用Google搜索,然后找到了这些站点:

I google it, and I found the sites:

  • 如何生成随机的字母数字字符串?
  • 如何在Java中生成JSON字符串?
  • How to generate a random alpha-numeric string?
  • How to generate JSON string in Java?

这些不是最佳答案.

在红宝石中,宝石伪造者可以生成伪造数据.

In ruby, the gem faker can generate fake data.

在Java中,哪个库可以生成假数据?哪个库可以生成伪造的JSON字符串?

推荐答案

您可以使用 mockneat 为了做到这一点.这是一个专门生成各种伪"数据的库.查看文档,看看可以伪造"什么以及如何伪造.

You can use mockneat in order to do that. It's a library specialised in generating all kind of "fake" data. Check out the documentation to see what you can "fake" and how.

有一个维基页面,向您展示了如何生成随机JSON :

There is a wiki page that shows you how you can generate a Random JSON:

MockNeat mockNeat = MockNeat.threadLocal(); Gson gson = new GsonBuilder() .setPrettyPrinting() .create(); String json = mockNeat .reflect(UserProfile.class) .field("name", mockNeat.names().full()) .field("userName", mockNeat.users()) .field("email", mockNeat.emails()) .field("profiles", mockNeat.reflect(Profile.class) .field("profileId", mockNeat.ints().range(100, 1000)) .field("profileAdded", mockNeat.localDates().toUtilDate()) .list(2)) .map(gson::toJson) /* Transforms the UserProfile class into a 'pretty' json. */ .val(); System.out.println(json);

给出的结果是(当然每次结果都不同):

And the given result is (of course, the results are different each time):

{ "name": "Cecila Starbird", "userName": "moistben", "email": "randiexyst@hotmail.co.uk", "profiles": [ { "profileId": 964, "profileAdded": "Mar 19, 1973 12:00:00 AM" }, { "profileId": 854, "profileAdded": "Jun 3, 1978 12:00:00 AM" } ] }

免责声明:我是图书馆的作者.

更多推荐

如何在Java中生成随机JSON字符串?

本文发布于:2023-11-09 20:55:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573408.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   如何在   Java   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!