導航:首頁 > 編程語言 > java傳輸json

java傳輸json

發布時間:2022-12-20 01:09:59

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");

閱讀全文

與java傳輸json相關的資料

熱點內容
dvd光碟存儲漢子演算法 瀏覽:757
蘋果郵件無法連接伺服器地址 瀏覽:963
phpffmpeg轉碼 瀏覽:671
長沙好玩的解壓項目 瀏覽:145
專屬學情分析報告是什麼app 瀏覽:564
php工程部署 瀏覽:833
android全屏透明 瀏覽:737
阿里雲伺服器已開通怎麼辦 瀏覽:803
光遇為什麼登錄時伺服器已滿 瀏覽:302
PDF分析 瀏覽:485
h3c光纖全工半全工設置命令 瀏覽:143
公司法pdf下載 瀏覽:382
linuxmarkdown 瀏覽:350
華為手機怎麼多選文件夾 瀏覽:683
如何取消命令方塊指令 瀏覽:350
風翼app為什麼進不去了 瀏覽:778
im4java壓縮圖片 瀏覽:362
數據查詢網站源碼 瀏覽:150
伊克塞爾文檔怎麼進行加密 瀏覽:892
app轉賬是什麼 瀏覽:163