Java错误使用扫描程序无限扫描扫描JSONObject程序

时间:2018-04-13 06:30:09

标签: java json parsing jsonparser

我在编写这个程序时遇到这个错误,我只是想通过扫描器将JSON对象传递给json对象

但扫描仪进入无限循环

package com.readvalues.main;

import java.util.Scanner;
import net.sf.json.JSONObject;

public class ReadValues 
{
    public static void main(String[] args)
    {
        String x="";
        try 
        {   
            System.out.println("Enter the data");
            Scanner scanner = new Scanner(System.in);
            while(scanner.hasNext())
            {
                x = x + scanner.next();
            }

            JSONObject jsonObject = JSONObject.fromObject(x);
            System.out.println(jsonObject);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

在终端

Enter the data
{"ExecutiveGoal":
{"EMPLID":"100020","GOAL_ID":"GOAL-3-98372","GOAL":"Conduct EPC Contract Kaizen","DUE_DATE":"07/01/2018","STATUS":"On Track or Complete (Green)","CATEGORY":"Operational Goal","SUPPORTS_ORG_GOAL":"","COMPLETED_ON":"06/30/2018","CREATED_BY":"Mark Hensley","CREATED_ON":"03/01/2018","LAST_UPDATED_BY":"Mark Hensley","LAST_UPDATED_ON":"03/01/2018"
},
"ExecutiveGoal":
{"EMPLID":"100020","GOAL_ID":"GOAL-3-98373","GOAL":"Exit 2018 with minimum 5 negotiators trained and capable in O&M, MSAs, EPCs, and MIPSAs. At least 2 negotiators capable in PPAs.","DUE_DATE":"12/31/2018","STATUS":"On Track or Complete (Green)","CATEGORY":"People Leader Goal","SUPPORTS_ORG_GOAL":"","COMPLETED_ON":"","CREATED_BY":"Mark Hensley","CREATED_ON":"03/01/2018","LAST_UPDATED_BY":"Mark Hensley","LAST_UPDATED_ON":"03/01/2018"
}
}
//INFINITE LOOP SCANNING 

我一直尝试

2 个答案:

答案 0 :(得分:1)

扫描仪正在等待... scanner.nextLine()会有所帮助。

答案 1 :(得分:0)

使用scanner.nextInt()时,它会等待整数值。你正在输入一个json字符串,这就是它抱怨的原因。您需要将其更改为需要字符串的scanner.next()

你写的循环也永远不会结束。它将要求您永远从输入中读取字符串。你可能会考虑一些"退出"条件;)