从控制台读取用户输入&把它写到JSON

时间:2016-03-02 07:17:49

标签: java arrays json gson

我是Java编程的新手。我想做的是,阅读控制台和放大器的输入;然后将其写入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: \"test@test.com\""    
            + "}";

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

    //System.out.println(user);
    return user;
}

现在,我希望控制台中的用户输入符合JSON。有人可以指导我如何实现这个目标吗?

感谢。

2 个答案:

答案 0 :(得分: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();
        }
    }
  

你必须导入       import org.json.JSONException;       import org.json.JSONObject;       import java.util.HashMap;       import java.util.Map;

答案 1 :(得分: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);

}