【问题标题】:Creating a website to convert text into Json file创建一个将文本转换为 Json 文件的网站
【发布时间】:2018-12-19 22:34:16
【问题描述】:

我正在尝试创建一个网站,供用户输入有关自己的信息并最终生成一个 Json 文件供他们下载。我可以使用 Java 创建它,但我需要一些帮助才能将它变成一个供用户使用的网站。

另外,如果有任何方法我可以只使用 JS、HTML 和 CSS 来做到这一点。

import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import org.json.simple.*;

public class JsonFile {
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {

        @SuppressWarnings("resource")
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter ID: ");
        int id_Input = scan.nextInt();
        System.out.println("Enter First Name: ");
        String firstname_Input = scan.next();
        System.out.println("Enter Last Name: ");
        String lastname_Input = scan.next();

        JSONObject patient = new JSONObject();
        patient.put("id", id_Input);
        patient.put("firstName", firstname_Input);
        patient.put("lastName", lastname_Input);

        System.out.println("Enter Father's First Name: ");
        String firstname_father = scan.next();
        System.out.println("Enter Father's Last Name: ");
        String lastname_father = scan.next();

        JSONObject father = new JSONObject();
        father.put("firstName", firstname_father);
        father.put("lastName", lastname_father);

        JSONArray list = new JSONArray();
        list.add(patient);
        list.add(father);

        try(FileWriter file = new FileWriter("testJSON.json")) {
            file.write(list.toString());
            file.flush();
        }
       catch(IOException e) {
            e.printStackTrace();
       }
    }
}

【问题讨论】:

  • 如果您使用 <input> 标签获取用户输入并将其值分配给值。然后在JS 中创建一个对象 例如:var jsonObj = {}; jsonObj.name = nameVar; jsonObj.age = ageVar; 等。然后使用JSON.stringify(jsonObj) 将其转换为字符串,然后您可以创建一个文件供他们下载。我没有把它放在官方答案中的原因是因为我现在没有时间自己测试它。

标签: javascript java html css json


【解决方案1】:

据我了解,您需要根据用户输入转换 JSON。使用我下面的示例代码。可能会对你有所帮助。

   <html>
<input type="text" id="txtid" value="Enter id"/>
<input type="text" id="txtfirstname" value="Enter first name"/>
<input type="text" id="txtlastname" value="Enter last name"/>
<input type="button" value="Display" id="btnDisplay" onclick="output()"/>

    <script>
        function output(){
            //Data collector
            var oData=[];
            //Local Data object initialization 
            var local={
                id:document.getElementById("txtid").value,
                firstname:document.getElementById("txtfirstname").value,
                lastname:document.getElementById("txtlastname").value
            };
            //Push data in data collector
            oData.push(local);
            //Convert data in json format string
            var output=JSON.stringify(oData).toString();
            //Data output
           document.write("<h1>"+ output +"</h1>")

           download("testfile.txt",output)
        }

        function download(filename, text) {
            var element = document.createElement('a');
            element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
            element.setAttribute('download', filename);
            element.style.display = 'none';
            document.body.appendChild(element);
            element.click();
            document.body.removeChild(element);
        }

    </script>
</html>

//**Paste this code in HTML file and run**.

//This is simple example for your reference only.

【讨论】:

  • 非常感谢@Abhishek Tomar,随着我在这个项目上的更多工作,我会及时通知您!这段代码肯定会帮助我开始:)
  • 如果这对您有帮助,请接受我的回答。点击我帖子前面的check。很高兴能提供帮助。
  • 刚刚做了 ;) 谢谢!
猜你喜欢
  • 2016-11-06
  • 2020-12-02
  • 1970-01-01
  • 2021-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-08
相关资源
最近更新 更多