我是Java編程的新手。我想要做的是,從控制檯&讀取輸入,然後將其寫入JSON,之後我會將數據保存到JSON數據庫中。從控制檯讀取用戶輸入並將其寫入JSON
我寫了一個類UserInput,它讀取用戶輸入如下:
package com.mmk.vo;
import java.io.*;
import java.util.Scanner;
import java.io.IOException;
public class UserInput {
private String firstName,lastName,email, mobile;
public String UserInput() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name;");
String firstName = sc.next();
Scanner sc1 = new Scanner(System.in);
System.out.println("Enter your lastName;");
String lastName = sc1.next();
Scanner sc2 = new Scanner(System.in);
System.out.println("Enter your email;");
String email = sc2.next();
Scanner sc3 = new Scanner(System.in);
System.out.println("Enter your mobile;");
String mobile = sc3.next();
return "UserInput [fisrtName=" + firstName + ", lastName=" + lastName + ", email=" + email + ", mobile="
+ mobile + "]";
}
}
我已經寫在我的主類,一個方法中,我已經給了一些默認值&這樣做是寫這些數據庫的默認值。
static UserInput jsonToDB(){
String json = "{"
+ "firstName: \"Arsene\","
+ "lastName: \"Wenger\","
+ "mobile: \"1234\","
+ "email: \"[email protected]\""
+ "}";
Gson g = new Gson();
UserInput user = g.fromJson(json, UserInput.class);
//System.out.println(user);
return user;
}
現在,我希望用戶從控制檯輸入信息進入JSON。有人能指導我如何實現這一目標嗎?
謝謝。
你想爲w rite json在System.out.println(user)上進行控制檯; ? –