In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article "java fastjson transmission long data received how to solve the int problem" most people do not understand, so the editor summed up the following content, detailed, clear steps, with a certain reference value, I hope you can get something after reading this article, let's take a look at this "java fastjson transmission long data received int problem how to solve" article.
Fastjson transmits long data but receives int
Recently, an interesting phenomenon has been found in the development of java. In a vo in network element A, there is a taskId field of type Long, which is converted into a json string with fastjson and transmitted to network element B with a http request. Network element B receives the entity with an map, but sends taskID its data type as Integer, as shown in figure 1 below.
At this point, if you use long taskId= (long) reqMap.get ("taskId"); save the field will report the following error java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long, if the taskId is greater than the integer range, what would be the scenario? The author makes the following attempt, first query the value of Integer.MAX_VALUE to 2147483647, then set the value of taskId to 3147483647 which is larger than it, and find that the data type accepted by network element B is Long, as shown in figure 2.
So how do we accept this data at the back end? I think of the parent class of Integer and Long, and find that she has public abstract int intValue (); public abstract long longValue (); so I wonder if I can use the parent class to accept it and then use longValue to convert it to long type. The post-test is feasible, and the results are shown in figure 3.
Summary: for long type, you can first receive it with Number, and then capture it as long,Number b = (Number) reqMap.get ("taskId"); taskId = b.longValue ()
Using FastJson for data type conversion
Because conversions between json data and objects, collections, map, and strings are often used in your work, make a summary.
Introduce a dependency of fastjosn
Com.alibaba fastjson 1.2.47
Java:
Data conversion to JSON
Note:
Since the collection and Map of scala are different from JAVA, java is used here as a case where the object is converted to json. If necessary, you can encapsulate the corresponding method, and then use scala to call it.
1) convert the object to json:
Write a user class and convert it as an object to a json case:
Package entity;public class user {private Integer id; private String username; private String password; public void setId (Integer id) {this.id = id;} public void setUsername (String username) {this.username = username;} public void setPassword (String password) {this.password = password;} public Integer getId () {return id;} public String getUsername () {return username } public String getPassword () {return password;} @ Override public String toString () {return "user {" + "id=" + id + ", username='" + username +'\'+ ", password='" + password +'\'+'}';}}
Convert an object to json in Map:
Package service.impl;import com.alibaba.fastjson.JSON;import entity.user;import java.util.HashMap;import java.util.Map;public class MapToJson {public static void main (String [] args) {user us = new user (); us.setId (1); us.setUsername ("lisa"); us.setPassword ("110"); user U2 = new user (); u2.setId (2) U2.setUsername ("julian"); u2.setPassword ("120s"); / * Map to JSON * / Map maps = new HashMap (); maps.put ("1", us); maps.put ("2", U2); System.out.println ("maps =" + JSON.toJSONString (maps));}}
Get the results:
Maps = {"1": {"id": 1, "password": "110"," username ":" lisa "}," 2 ": {" id ": 2," password ":" 120", "username": "julian"}}
2) convert the string to json
Convert a string to json, because the language used in data processing is scala, so scala is used as a case:
/ * two. Convert a string to json * / case class student (id:Int, name:String) var maps=Map [String,student] () val stu1=student (1, "lisa") val stu2=student (2, "july") maps+= ("1"-> stu1) maps+= ("2"-> stu2) val result2: String = JSON.toJSONString (maps,SerializerFeature.PrettyFormat) println (result2)
Result: {"rule1": "kasa", "rule2": "uzi"}
Parse json data / * * one. Parsing json data Get a specific value * / parse multi-tier nested data: val str = "{\" error\ ": 200,\" msg\ ": null,\" tianji_api_agentn_fenqiscorev10_response\ ": {\" tags\ ": 0,\" 151l\ ": 0,\" 1515\ ": 0,0,1153,0,1154,0,110\": 0,111\ ": 0,112\": 1. \ "113\": 0,\ "114\": 0,\ "116\": 0,\ "117\": 0,\ "118\": 0,\ "119\": 3,\ "161\": 3,\ "162\": 1,\ "120\": 0,121\ ": 5,\" 122\ ": 0,\" 123\ ": 0,\" 124\ ": 0,\" 125\ ": 2,\" 5\ ":\" android\ " \ "126\": 0,\ "127\": 4,\ "128\": 0,\ "129\": 0,\ "130\": 0,\ "131\": 0,\ "132\": 0,\ "133\": 0,\ "134\": 0,\ "135\": 0,\ "136\": 0,\ "137\": 0,\ "138\": 0,\ "139\": 0,\ "140\": 3,\ "141\": 0 \ "142\": 5,\ "143\": 3,\ "144\": 0,\ "101\": 0,\ "145\": 6,\ "102\": 1,\ "146\": 0,\ "103\": 1,\ "147\": 0,\ "148\": 0,\ "104\": 0,\ "149\": 0,\ "105\": 3,\ "106\": 5,\ "107\": 0,\ "108\": 2 2,\ "credit_score\": 457},\ "biz_id\":\ "2589e393-004d-4b37-b0ab-23455c4bbde2\",\ "req_id\":\ "15422508480665693989\"} \ "request_id\":\ "15422508480517778031\"} "/ / convert the string to jsonobject val json: JSONObject = JSON.parseObject (str) / / get a value from the key and get a jsonobject val tj: JSONObject = json.getJSONObject (" tianji_api_agentn_fenqiscorev10_response ") / / get the valueval tag=tj.getJSONObject of the corresponding key (" tags ") according to the previous jsonobject / / get the valueval result=tag.getString of the specific key of the multi-level nested json ( Println (result) is the content of the article on "how to solve the problem that java fastjson transmits long data and receives int" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.