IDEA Android studio toString() 生成Json模板

toString → Settings
在这里插入图片描述

public java.lang.String toString() {
final java.lang.StringBuilder sb = new java.lang.StringBuilder("{");
#set ($i = 0)
#foreach ($member in $members)#if ($i == 0)
sb.append("#####
#else
sb.append(",####
#end#if ($member.string || $member.date)
\"$member.name\":\"")
#else
\"$member.name\":")
#end#if ($member.primitiveArray || $member.objectArray)
.append(java.util.Arrays.toString($member.name));
#elseif ($member.string || $member.date)
.append($member.accessor).append('\"');
#else
.append($member.accessor);
#end#set ($i = $i + 1)
#end
sb.append('}');
return sb.toString();
}

或者

public java.lang.String toString() {
return "{"+
#set ($i = 0)
#foreach ($member in $members)
#if ($i == 0)
"##
#else
",##
#end
#if ($member.primitiveArray || $member.objectArray)
\"$member.name\":"+java.util.Arrays.toString($member.name)+
#elseif ($member.string || $member.date)
\"$member.name\":\""+$member.accessor+('\"')+
#else
\"$member.name\":"+$member.accessor+
#end
#set ($i = $i + 1)
#end
'}';
}

效果如下:

在这里插入图片描述

发布了136 篇原创文章 · 获赞 47 · 访问量 37万+
展开阅读全文

String字符串转换成json格式并打印json数据。

09-04

执行到150行的时候,不抱错,也没有执行后面的语句,我想请教大神String转json格式然后存储到本地数据库,有什么办法。 package sql_conn; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import javax.swing.*; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.sql.Types; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.RandomAccessFile; import java.util.ArrayList; import java.util.List; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class java_sql extends JFrame{ JTextArea jtf1,jtf2; JButton yellowbutton,bluebutton,redbutton,okbutton; private static String test_url="jdbc:jtds:sqlserver://192.168.5.14:1433;DatabaseName=EUCP5"; private static String test_user = "admin123"; private static String test_pwd = "abcd_12345"; private static Connection con = null; public static void main(String[] args) { // TODO Auto-generated method stub EventQueue.invokeLater(new Runnable() { @Override public void run() { // TODO Auto-generated method stub java_sql frame=new java_sql(); frame.setVisible(true); } }); } public java_sql(){ this.setTitle("EUCP_数据库连接调试工具"); this.setSize(600,600); okbutton=new JButton("提交"); okbutton.setVisible(true); /*按钮添加button1Handler监听*/ okbutton.addActionListener(new Button1Handler()); //add buttons to panel buttonPanel=new JPanel(); buttonPanel.add(okbutton); this.add(buttonPanel); } private class Button1Handler implements ActionListener{ public void actionPerformed(ActionEvent e){ try { java_sql sms=new java_sql(); //jtf2.setText(okbutton.getActionCommand()); //JOptionPane.showMessageDialog(null, e.toString(), "alter", JOptionPane.ERROR_MESSAGE); sms.send_middle("177612309","验证码是48907"); } catch (Exception ex) { //JOptionPane.showMessageDialog(null, ex.toString(), "alter", JOptionPane.ERROR_MESSAGE); } } } private JPanel buttonPanel; public static int send_middle(String url, String param) throws MalformedURLException, UnsupportedEncodingException { String inputLine = ""; int value = -2; String con; DataOutputStream out = null; //InputStream in = null; String strUrl="http://eucpwx.mb345.com:443/AppApi/User/GetUserList?"; String strparam = "UserID=SU0126&GetType=1&NowID=&GetNum=100&Other=&SearchUserName="; try { System.out.println("start"); inputLine = sendPost(strUrl, strparam); //value = new Integer(inputLine).intValue(); System.out.println(inputLine); String str=String.valueOf(inputLine); //JSONObject st = JSONObject.fromObject(str); JSONObject jsonObject = new JSONObject().fromObject(str.toString()); System.out.println("可以"); Object data=jsonObject.get("UserID"); jsonObject = new JSONObject().fromObject(data.toString()); data=jsonObject.get("data"); jsonObject = new JSONObject().fromObject(data.toString()); data=jsonObject.get("value"); int value_k = Integer.parseInt(data.toString()); /* Statement stmt = null; String strSQL = ""; String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"; String dbURL="jdbc:slserver://192.168.5.14:1433;DatabaseName=EnterpriseSMS_Play"; String userName="admin"; String userPwd="abcd"; Class.forName(driverName); Connection dbConn=DriverManager.getConnection(dbURL,userName,userPwd); String sql = ""; //注意java对SQL Server发送的SQL语句中表名需要用[]包围 System.out.println("SQL 开始"); for (int i = 0; i < jsonArr.size(); i++) { userid[i] = jsonArr.getJSONObject(i).getString("userid"); username[i] = jsonArr.getJSONObject(i).getString("username"); mobile[i] = jsonArr.getJSONObject(i).getString("mobile"); Avatar[i] = jsonArr.getJSONObject(i).getString("Avatar"); sql = "INSERT into user_bak (userid, username, mobile, Avatar) values('"+userid[i]+"','" +username[i]+"','"+mobile[i]+ "','"+Avatar[i]+"');"; System.out.println(sql.toString()); PreparedStatement statement = null; statement = dbConn.prepareStatement(sql); ResultSet rs = statement.executeQuery(); rs.close(); statement.close(); dbConn.close(); File f = new File("log.txt"); if (!f.exists()) { f.createNewFile(); } OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f),"utf-8"); BufferedWriter writer=new BufferedWriter(write); writer.write(content); writer.close(); }*/ } catch (Exception e) { //System.out.println(e.toString()); value = -2; } //System.out.println(String.format("返回值:%d", value)); return value; } /** * 向指定 URL 发送POST方法的请求 * * @param url * 发送请求的 URL * @param param * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 * @return 所代表远程资源的响应结果 */ public static String sendPost(String url, String param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); // 打开和URL之间的连接 URLConnection conn = realUrl.openConnection(); // 设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); // 获取URLConnection对象对应的输出流 out = new PrintWriter(conn.getOutputStream()); // 发送请求参数 out.print(param); // flush输出流的缓冲 out.flush(); // 定义BufferedReader输入流来读取URL的响应 in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { //System.out.println(line); //String send_line =URLEncoder.encode(line.toString(), "utf-8"); //JOptionPane.showMessageDialog(null, line.toString(), "alter", JOptionPane.ERROR_MESSAGE); //System.out.println(line.toString()); result += line; //System.out.println(result.toString()); } } catch (Exception e) { //System.out.println("发送 POST 请求出现异常!" + e); e.printStackTrace(); } // 使用finally块来关闭输出流、输入流 finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result; } } ![图片说明](https://img-ask.csdn.net/upload/201709/04/1504509813_211448.png) 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览