gpt4 book ai didi

java - JSON 简单的意外字符?

转载 作者:行者123 更新时间:2023-12-05 03:51:26 28 4
gpt4 key购买 nike

Unexpected character () at position 0.
at org.json.simple.parser.Yylex.yylex(Yylex.java:610)
at org.json.simple.parser.JSONParser.nextToken(JSONParser.java:269)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:118)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:81)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:75)
at net.ddns.coolpvp.Testing.main(Testing.java:22)

我在 Java 上制作了一个 TCP 服务器,它正在接收一个 json 并给出了这个错误,但我检查了第一个字符是'{',我该如何解决这个问题?我没有线索。如果您能帮助我,我将不胜感激
编辑:JSON 是由 C# 应用程序中的 .NET Framework 生成的,这是一个 JSON

{"Type":"level-info","LevelNumber":1}

这就是 C# 应用程序生成 JSON 的方式
程序.cs

using System;
using System.Text;
using System.Net.Sockets;
using System.IO;

namespace Testing
{
public static class Program
{
public static void Main(string[] args)
{
TcpClient client = new TcpClient();
client.Connect("localhost", 152);
StreamWriter writer = new StreamWriter(client.GetStream(), Encoding.UTF8) { AutoFlush = true };
writer.WriteLine(new RequestLevelInfo(1).ToJSONString());
client.Close();
Console.ReadKey(true);
}
}
}

RequestLevelInfo.cs

using System.Web.Script.Serialization;

namespace Testing
{
public class RequestLevelInfo
{
public string Type { get { return "level-info"; } }
public int LevelNumber { get; }
public RequestLevelInfo(int level)
{
LevelNumber = level;
}
public string ToJSONString()
{
return new JavaScriptSerializer().Serialize(this);
}
}
}

服务器正在使用 BufferedReader 使用 readLine 方法读取它

package testing;

import java.net.Socket;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class Testing {
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket();
server.bind(new InetSocketAddress(InetAddress.getByName("localhost"), 152));
Socket client = server.accept();
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(), "UTF-8"));
String dataReceived = reader.readLine();
JSONObject json = (JSONObject)new JSONParser().parse(dataReceived);
System.out.println(json.toJSONString());
client.close();
server.close();
} catch (IOException ex) {
ex.printStackTrace(System.err);
} catch (ParseException ex) {
ex.printStackTrace(System.err);
}
}
}

最佳答案

问题出在您的 C# 代码中:它发送的 JSON 不正确。

您正在使用 Encoding.UTF8 对象。该对象包含一个不可见且不必要的“字节顺序标记”字符,Java JSON 解析器无法理解该字符。 JSON“不得”使用字节顺序标记字符:JSON Specification and usage of BOM/charset-encoding

解决方案是创建您自己的 UTF8Encoding 实例。例如:

UTF8Encoding jsonEncoding = new UTF8Encoding(false);
StreamWriter writer = new StreamWriter(client.GetStream(), jsonEncoding) { AutoFlush = true };

关于java - JSON 简单的意外字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62880986/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com