Ⅰ java后台怎样向前台传一个json数据
假设后台传递到前台数据的临时对象为json(你上面那一长串)。 var json = “{……}”; json = eval('(' + json + ')'); json就对前台对象了,json.endPlncre可以拿出指定属性 385
Ⅱ java中使用json传递数据,丢失精度了,求各位大神指点一二一下如何解决json精度问题
把你传的数值类型数据转成字符串再传递就行了
现在是
{"dt":"1 ","evaporate":2,"rainfall":2.5999999046325684}
改成
{"dt":"1 ","evaporate":2,"rainfall":"2.5999999046325684"}
如果类rainwater是你自己定义的,就比较简单了,吧Rainfall属性的类型从double改成String
如果不是就只能在数据转json前先遍历整个集合,然后转字段类型了
Ⅲ 如何用Java向kafka发送json数据
发送json也可以看成字符串处理
We have 2 Options as listed below
1) If we intend to send custom java objects to procer, We need to create a serializer which implements org.apache.kafka.common.serialization.Serializer and pass that Serializer class ring creation of your procer
Code Reference below
public class PayloadSerializer implements org.apache.kafka.common.serialization.Serializer {
public void configure(Map map, boolean b) {
}
public byte[] serialize(String s, Object o) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(o);
oos.close();
byte[] b = baos.toByteArray();
return b;
} catch (IOException e) {
return new byte[0];
}
}
public void close() {
}
}
And set the value serializer accordingly
<entry key="value.serializer"
value="com.spring.kafka.PayloadSerializer" />
2) No need to create custom serializer class. Use the existing ByteArraySerializer, but ring send follow the process
Java Object -> String (Preferrably JSON represenation instead of toString)->byteArray
Ⅳ java中json怎么运用
json一般都是配合ajax一起使用的 我做做过的小例子 粘给你 你可以研究一下
js部分
//获取卡的金额
function get_money(){
var str=document.getElementById("pk_card_type").value;
//alert(str);
var url = '/member_h.do';
var pars = 'method=getMoney';
pars+='&pk_card_type='+str;
var ajax = new Ajax.Request(
url,
{method:'post',parameters:pars,onComplete:show_money}
);
}
//回调函数 写入卡的金额
function show_money(dataResponse)
{
var data = eval('(' + dataResponse.responseText + ')');
var price=0;
price=data.price;
var collection_fees=0;
collection_fees=data.collection_fees;
document.getElementById("recharge").value=price;
document.getElementById("collection_fees").value=collection_fees;
}
action部分
public ActionForward getMoney(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
response.setContentType("text/html; charset=utf-8");
try {
IElementaryFileService ggsv = new ElementaryFileService();
String pk_card_type = request.getParameter("pk_card_type");
Card_TypeVO ctvo=new Card_TypeVO();
ctvo=ggsv.queryByPK(Card_TypeVO.class, pk_card_type);
PrintWriter out = response.getWriter();
// 这里的数据拼装一般是从数据库查询来的
JSONObject jsonObject = new JSONObject();
if(ctvo!=null){
jsonObject.put("price", ctvo.getCard_money());
jsonObject.put("collection_fees", ctvo.getCash());
}else{
jsonObject.put("price", 0);
jsonObject.put("collection_fees", 0);
}
out.print(jsonObject.toString());
out.flush();
out.close();
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
Ⅳ java中如何用json格式发送并接受arrayList
这个要用到json的jar包json-lib-2.x ,然后用JSONArray对象封装list,最后把jsonarray放入jsonobject中封装成json对象。当然了 如果你用框架的话人家有封装json对象的机制。原生态servlet就自己用json包封装呗 给段例子给你刚写的:
Ⅵ java的IO流怎么发送json 请具体分步哦,打好了有悬赏。
首先封装的数据,MAP/String/XML等等格式。举个例子
String putData = "这是我要发送的数据";
JSONObject json= new JSONObject();
json.put("datas",putData);
PrintWriter out = response.getWriter();
out.println(json.toString());
Ⅶ java json传值过程
package test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
public class ServletTest extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String name=req.getParameter("name");
String pwd=req.getParameter("pwd");
JSONObject json=new JSONObject();
json.put("name", name);
json.put("pwd", pwd);
PrintWriter out=resp.getWriter();
out.write(json.toString());
out.flush();
out.close();
}
}
Ⅷ java用服务端怎么用json数据传输到安卓客户端
java用服务端怎么用json数据传输到安卓客户端
public class MainActivity extends Activity
{
private Button submit = null;
URL url = null;
String urlPath = "http://10.0.3.2:8080/XMLParse/AcceptJsonServlet";
EditText name = null;
EditText age = null;
EditText address = null;
Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
if (msg.what == 0x123)
{
Toast.makeText(MainActivity.this,
"发送成功", Toast.LENGTH_LONG)
.show();
}
else
{
Toast.makeText(MainActivity.this,
"发送失败", Toast.LENGTH_LONG)
.show();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = ((EditText) findViewById(R.id.name));
age = ((EditText) findViewById(R.id.age));
address = ((EditText) findViewById(R.id.address));
submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
new Thread()
{
public void run()
{
JSONObject js = new JSONObject();
JSONObject params = new JSONObject();
// JSONArray array = new JSONArray();
Person p = new Person(name.getText().toString(), age
.getText().toString(), address.getText()
.toString());
// 封装子对象
try
{
js.put("name", p.getName());
js.put("age", p.getAge());
js.put("address", p.getAddress());
// 封装Person数组
params.put("Person", js);
}
catch (JSONException e)
{
e.printStackTrace();
}
// 把Json数据转换成String类型,使用输出流向服务器写
final String content = String.valueOf(params);
try
{
url = new URL(urlPath);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setConnectTimeout(5000);
conn.setDoOutput(true);// 设置允许输出
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"application/json; charset=UTF-8"); // 内容类型
OutputStream os = conn.getOutputStream();
os.write(content.getBytes());
os.close();
if (conn.getResponseCode() == 200)
{
handler.sendEmptyMessage(0x123);
}
else
{
handler.sendEmptyMessage(0x122);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}.start();
Ⅸ java如何接收ajax传递的json类型的数据
主要是赋值给Map就行。
json数据格式为:
{name:'张三',age:20}这样的不是数组时,
在java中new一个Map对象获取,如
Map<String,String> map = (Map<String,String>)request.getParament("json");
然后获取name为:map.get("name");
如果json数据为数组:
[{name:'张三',age:17},{name:'李四',age:20}]
在java中new一个
List<Map<String,String> maplist = (List<Map<String,String>)request.getParament("json");