本文整理汇总了Java中com.alibaba.fastjson.JSON类的典型用法代码示例。如果您正苦于以下问题:Java JSON类的具体用法?Java JSON怎么用?Java JSON使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JSON类属于com.alibaba.fastjson包,在下文中一共展示了JSON类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import com.alibaba.fastjson.JSON; //导入依赖的package包/类
@Test
public void main() throws IOException {
File a = FileUtils.getFile("D:/a.txt");
String str = FileUtils.readFileToString(a,"utf-8");
JSONObject json = JSON.parseObject(str);
JSONArray arry = JSONArray.parseArray(json.get("delegates").toString());
for(int i = 0;i<arry.size();i++){
JSONObject obj= (JSONObject) arry.get(i);
String username = obj.getString("username");
String productivity = obj.getString("productivity");
String producedblocks = obj.getString("producedblocks");
String balance = obj.getString("balance");
String approval = obj.getString("approval");
System.out.println(username +"||" + productivity +"||" + producedblocks + "||" + balance + "||" + approval);
}
}
示例2: main
import com.alibaba.fastjson.JSON; //导入依赖的package包/类
public static void main(String[] args) {
PropertyConfigurator.configure("conf/log4j.properties");
Method[] methods = HelloImpl.class.getDeclaredMethods();
for (Method method : methods) {
System.out.println(method.getName());
Class<?>[] param = method.getParameterTypes();
System.out.println(JSON.toJSONString(param));
try {
Method m2 = RMethodUtils.searchMethod(HelloImpl.class, method.getName(), param);
if (m2 == null) {
System.out.println("null");
} else {
System.out.println(method.getName());
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
示例3: test_min
import com.alibaba.fastjson.JSON; //导入依赖的package包/类
public void test_min() throws Exception {
Random random = new Random();
Model[] array = new Model[2048];
for (int i = 0; i < array.length; ++i) {
array[i] = new Model();
array[i].value = random.nextLong();
}
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);
}
}
示例4: map2ObjectTest
import com.alibaba.fastjson.JSON; //导入依赖的package包/类
@Test
public void map2ObjectTest() {
Map<String, String> result = mockRefundResult();
String json = JSON.toJSONString(result);
System.out.println("json:" + json);
AlipayRefundResult refund = JSON.parseObject(json, AlipayRefundResult.class);
System.out.println("refund:" + refund);
String value = StringUtil.toJoinForSign(refund, Constants.UTF_8, false);
System.out.println("value:" + value);
String text = JSON.toJSONString(refund);
System.out.println("text:" + text);
System.out.println("map:" + JSON.parseObject(text, Map.class));
System.out.println("refund2:"+StringUtil.toJoinForSign(JSON.parseObject(text, Map.class),Constants.UTF_8,false));
}
示例5: test_boolean
import com.alibaba.fastjson.JSON; //导入依赖的package包/类
public void test_boolean() throws Exception {
Assert.assertEquals(Boolean.TRUE, JSON.parseObject("true", Boolean.class));
Assert.assertEquals(Boolean.FALSE, JSON.parseObject("false", Boolean.class));
Assert.assertEquals(Boolean.TRUE, JSON.parseObject("'true'", Boolean.class));
Assert.assertEquals(Boolean.FALSE, JSON.parseObject("'false'", Boolean.class));
Assert.assertEquals(Boolean.TRUE, JSON.parseObject("1", Boolean.class));
Assert.assertEquals(Boolean.FALSE, JSON.parseObject("0", Boolean.class));
Assert.assertEquals(null, JSON.parseObject("null", Boolean.class));
{
DefaultJSONParser parser = new DefaultJSONParser("null", ParserConfig.getGlobalInstance(),
JSON.DEFAULT_PARSER_FEATURE);
Assert.assertEquals(null, BooleanCodec.instance.deserialze(parser, null, null));
parser.close();
}
Assert.assertEquals(JSONToken.TRUE, BooleanCodec.instance.getFastMatchToken());
}
示例6: indices
import com.alibaba.fastjson.JSON; //导入依赖的package包/类
public String indices() {
String uri = "/_cat/indices?v";
String result = base(uri, HttpUtil.METHOD_GET, null);
if(!StringUtil.isEmpty(result)){
String[] data = result.split("\n");
if(data!=null&&data.length>1){
List<JSONObject> list = new ArrayList<JSONObject>();
String[] header = data[0].split("(\\s+)");
for(int i=1;i<data.length;i++){
String[] values = data[i].split("(\\s+)");
JSONObject json = new JSONObject();
for(int j=0;j<values.length;j++){
json.put(header[j], values[j]);
}
list.add(json);
}
if(list!=null&&list.size()==1){
return list.get(0).toJSONString();
}else{
return JSON.toJSONString(list);
}
}
}
return result;
}
示例7: build
import com.alibaba.fastjson.JSON; //导入依赖的package包/类
@Override
public RpcAcsRequest build() {
com.aliyuncs.cloudphoto.model.v20170711.MergeFacesRequest request = new com.aliyuncs.cloudphoto.model.v20170711.MergeFacesRequest();
request.setProtocol(ProtocolType.HTTPS); //指定访问协议
request.setAcceptFormat(FormatType.JSON); //指定api返回格式
request.setMethod(MethodType.POST); //指定请求方法
request.setRegionId(REGION_CN);//指定要访问的Region,仅对当前请求生效,不改变client的默认设置。
request.setTargetFaceId(targetId);
request.setFaceIds(faceIds);
if (!stsToken.isEmpty())
request.setSecurityToken(stsToken);
else if (!libraryId.isEmpty())
request.setLibraryId(libraryId);
request.setStoreName(storeName);
request.setActionName("MergeFaces");
return request;
}
示例8: test_for_issue
import com.alibaba.fastjson.JSON; //导入依赖的package包/类
public void test_for_issue() throws Exception {
DubboResponse resp = new DubboResponse();
// test for JSONObject
JSONObject obj = new JSONObject();
obj.put("key1","value1");
obj.put("key2","value2");
resp.setData(obj);
String str = JSON.toJSONString(resp);
System.out.println(str);
DubboResponse resp1 = JSON.parseObject(str, DubboResponse.class);
assertEquals(str, JSON.toJSONString(resp1));
// test for JSONArray
JSONArray arr = new JSONArray();
arr.add("key1");
arr.add("key2");
resp.setData(arr);
String str2 = JSON.toJSONString(resp);
System.out.println(str2);
DubboResponse resp2 = JSON.parseObject(str2, DubboResponse.class);
assertEquals(str2, JSON.toJSONString(resp2));
}
示例9: onPostExecute
import com.alibaba.fastjson.JSON; //导入依赖的package包/类
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (!TextUtils.isEmpty(result)) {
String jsonString = JSON.parseObject(result).getString(Constant.SUC_MSG);
if (jsonString != null) {
mDataList = JSON.parseArray(jsonString, FavorPrivateInfo.class);
mRecyclerViewAdapter = new RecyclerViewAdapter(getActivity(), mDataList);
mPullLoadMoreRecyclerView.setAdapter(mRecyclerViewAdapter);
mRecyclerViewAdapter.notifyDataSetChanged();
mPullLoadMoreRecyclerView.setPullLoadMoreCompleted();
} else {
//Toast.makeText(QianXunApplication.getInstance(), JSON.parseObject(result).getString(Constant.FAIL_MSG), Toast.LENGTH_LONG).show();
mPullLoadMoreRecyclerView.setPullLoadMoreCompleted();
return;
}
}
}
示例10: executeSql
import com.alibaba.fastjson.JSON; //导入依赖的package包/类
@GET
@Path("/executeSql")
public Response executeSql(Map<String, Object> map) {
try {
DbusDataSource ds = new DbusDataSource();
ds.setDsType(map.get("dsType").toString());
ds.setMasterURL(map.get("URL").toString());
ds.setDbusUser(map.get("user").toString());
ds.setDbusPassword(map.get("password").toString());
TableFetcher fetcher = TableFetcher.getFetcher(ds);
List<Map<String,Object>> list = fetcher.fetchTableColumn(map);
return Response.ok().entity(list).build();
} catch (Exception e) {
logger.error("Error encountered while search data table with parameter:{}", JSON.toJSONString(map), e);
return Response.status(200).entity(new Result(-1, e.getMessage())).build();
}
}
示例11: send
import com.alibaba.fastjson.JSON; //导入依赖的package包/类
/**
* Send DanMu.
*
* @param color color of DanMu
* @param fontSize font size of DanMu
* @param mode DanMu mode
* @param message DanMu content
* @return server response entity
*/
public DanMuResponseEntity send(String color, String fontSize, String mode, String message) throws IOException, IllegalArgumentException {
resolveRoomData();
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://live.bilibili.com/msg/send");
httpPost.setHeader("Cookie", cookies);
httpPost.setEntity(
new UrlEncodedFormEntity(
Arrays.asList(
new BasicNameValuePair("color", color),
new BasicNameValuePair("fontsize", fontSize),
new BasicNameValuePair("mode", mode),
new BasicNameValuePair("msg", message),
new BasicNameValuePair("random", random.toString()),
new BasicNameValuePair("roomid", roomId.toString())
),
StandardCharsets.UTF_8
)
);
DanMuResponseEntity danMuResponseEntity = JSON.parseObject(EntityUtils.toString(closeableHttpClient.execute(httpPost).getEntity()), DanMuResponseEntity.class);
closeableHttpClient.close();
return danMuResponseEntity;
}
示例12: testOKHttpGetListForce
import com.alibaba.fastjson.JSON; //导入依赖的package包/类
void testOKHttpGetListForce() {
dlg.show();
ArrayList<RequestParameter> params = new ArrayList<RequestParameter>();
RequestParameter rp1 = new RequestParameter("cityId", "1");
params.add(rp1);
RequestParameter rp2 = new RequestParameter("showdate", "20161101");
params.add(rp2);
RemoteService.getInstance().invoke("getMovieList", params, new AbstractRequectCallback() {
@Override
public void onSuccess(String content) {
dlg.dismiss();
MovieList entity = JSON.parseObject(content, MovieList.class);
tvMovie.setText(entity.movieList.get(0).movieName);
}
}, true);
}
示例13: getPowerVOByUserId
import com.alibaba.fastjson.JSON; //导入依赖的package包/类
@Override
public List<PowerVO> getPowerVOByUserId(int userId) {
Power powerDTO = powerMapper.selectByUserId(userId);
List<PowerVO> listDTO = powerTypeService.listPowerVO();
if(powerDTO!=null){
List<Integer> powerListDTO = JSON.parseArray(powerDTO.getPower(), Integer.class);
for(PowerVO p1: listDTO){
for(int p2: powerListDTO){
if(p1.getId()==p2){
p1.setPermit(1);
if(p1.getMaxLv()!=0){
FormManager formManagerDTO = formManagerService.getFormManagerByApprover(userId);
List<Integer> lvListDTO = JSON.parseArray(formManagerDTO.getApproverForm(), Integer.class);
int formTypeDTO = powerTypeService.getPowerTypeByPowerId(p2).getFormType();
try {
p1.setPermit(lvListDTO.get(formTypeDTO - 1));
}catch (Exception e){
e.printStackTrace();
}
}
}
}
}
}
return listDTO;
}
示例14: processStringMessage
import com.alibaba.fastjson.JSON; //导入依赖的package包/类
private void processStringMessage(String msg) {
logger.info("get Message {}", msg);
/**
* 处理GMsession
*/
JSONObject json = JSON.parseObject(msg);
ActorRef actorRef = null;
String COMMAND_ID = json.getString("id");
if (gmSession.containsKey(COMMAND_ID)) {
actorRef = gmSession.get(COMMAND_ID);
} else {
gmSession.put(COMMAND_ID, actorRef);
}
CacheManager component = ContextResolver.getComponent(CacheManager.class);
ActorRef localPath = component.getActorGlobalPath(ClusterDistributedPub.IDENTIFY);
localPath.tell("ssss", ActorRef.noSender());
actorRef.tell(msg, getSender());
}
示例15: test5
import com.alibaba.fastjson.JSON; //导入依赖的package包/类
@RequestMapping("test5")
public
@ResponseBody
String test5(@RequestBody FastJsonEnumTestVO vo) {
return JSON.toJSONString(vo);
}