Java中json数据问题.老报错 A JSONObject text must begin with '{'

jiayouchong jiayouchong 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
json数据如下:
{"success":true,"msg":"成功"}


代码如下:

package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import net.sf.json.JSONObject;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {

StringBuffer sb = new StringBuffer();
try {
File file = new File("e:\\test.txt");
InputStreamReader read = new InputStreamReader(new FileInputStream(
file),"UTF-8");// 考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
sb.append(lineTxt);
}
} catch (Exception e) {
e.printStackTrace();
}

String json =sb.toString();
System.out.println(json);
JSONObject jsonobject=JSONObject.fromObject(json);
System.out.println(jsonobject);
}
}


错误如下:

{"success":true,"msg":"成功"}
Exception in thread "main" net.sf.json.JSONException: A JSONObject text must begin with '{' at character 1 of {"success":true,"msg":"成功"}
at net.sf.json.util.JSONTokener.syntaxError(JSONTokener.java:527)
at net.sf.json.JSONObject._fromJSONTokener(JSONObject.java:1112)
at net.sf.json.JSONObject._fromString(JSONObject.java:1337)
at net.sf.json.JSONObject.fromObject(JSONObject.java:187)
at net.sf.json.JSONObject.fromObject(JSONObject.java:156)
at test.Test.main(Test.java:34)

新人,求指导
...全文
评论 8
收藏 已收藏
分享
返回顶部
jiayouchong
Java EE 67,535 成员 225,843 内容