2016-03-02 192 views
0

我是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。有人能指導我如何實現這一目標嗎?

謝謝。

+0

你想爲w rite json在System.out.println(user)上進行控制檯; ? –

回答

1
**Try this just copy and paste it in your public class** 

    public static void main(String... args) 
    { 
     Map<String,String> myMap = new HashMap<String,String>(); 

     Scanner sc = new Scanner(System.in); 
     System.out.println("Enter your name;"); 
     String firstName = sc.next(); 
     myMap.put("firstName",firstName); 


     Scanner sc1 = new Scanner(System.in); 
     System.out.println("Enter your lastName;"); 
     String lastName = sc1.next(); 
     myMap.put("lastName",lastName); 

     Scanner sc2 = new Scanner(System.in); 
     System.out.println("Enter your email;"); 
     String email = sc2.next(); 
     myMap.put("email",email); 

     Scanner sc3 = new Scanner(System.in); 
     System.out.println("Enter your mobile;"); 
     String mobile = sc3.next(); 
     myMap.put("mobile",mobile); 


     try { 
      JSONObject jsonObject = new JSONObject(myMap.toString()); 
      System.out.println(jsonObject.toString()); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 

你必須輸入 進口org.json.JSONException; import org.json.JSONObject; import java.util.HashMap; import java.util.Map;

1

有很多,可能你的代碼加以改進,但解決的主要問題:

在你UserInput類,你已經宣佈你沒有在「UserInput」的方法設置類的屬性。您的UserInput方法可能應該重命名爲'readUser',因爲'UserInput'通常會指示UserInput類的構造方法。

所以我建議改變你的UserInput方法的名稱,並使其爲靜態。也使建立一個新的UserInput對象並設定它從控制檯到新UserInput對象的屬性讀取值 - 如下:

public static UserInput readUser() { 

    UserInput userInput = new UserInput(); //construct a new UserInput instance 

    Scanner sc = new Scanner(System.in); 
    System.out.println("Enter your name;"); 
    userInput.firstName = sc.next(); // set the value read from console as a property on the UserInput instance 

    ... etc. 

    return userInput; // return the instance of UserInput object that we just created. 


} 

然後在你的jsonToDb()方法 - 採取「UserInput」對象作爲參數: 即

public static UserInput jsonToDb(UserInput userInput) { 

    Gson g = new Gson(); 

    String json = userInput.toJson(userInput); 

    ..... 
    UserInput user = g.fromJson(json, UserInput.class); 

} 

或交替不傳遞UserInput對象 - 使用靜態方法從控制檯讀取它:

public static UserInput jsonToDb() { 

    UserInput userInput = UserInput.readUser(); 
    Gson g = new Gson(); 

    String json = userInput.toJson(userInput); 

    ..... 
    UserInput user = g.fromJson(json, UserInput.class); 

} 
相關問題