gpt4 book ai didi

c# - 使用 JSON 响应从 Java 调用 C# Web 服务

转载 作者:行者123 更新时间:2023-12-01 14:04:47 24 4
gpt4 key购买 nike

在我们的工作编程环境中,我们有 Java 和 C# 开发人员。我有一个用 C# 创建的 Web 服务,Java 开发人员正在尝试使用它。我一直在编写 Java 来使用此 Web 服务,虽然我得到了 json 结果,但它的格式错误。

这是我在 C# 方面的内容:

[WebMethod]
public static LinkedList<string> GetInfo(string InfoID, string Username, string Password)
{
LinkedList<string> Result = new LinkedList<string>();
try
{
// Do some stuff I can't show you to get the information...
foreach (Result from data operations)
{
Result.AddLast(sample1);
Result.AddLast(sample2);
Result.AddLast(sample3);
Result.AddLast(BD));
Result.AddLast(CN);
Result.AddLast(Name);
Result.AddLast("###");
}
}catch(Exception exc)
{
Result.AddLast(exc.ToString());
return Result;
}
return Result;
}

那么这是 Java 端:

try {
String uri = "http://example.com/service.asmx/GetInfo";

URL url = new URL(uri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

// Setup Connection Properties
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Accept", "application/json");
connection.setChunkedStreamingMode(0);
connection.connect();

// Create the JSON Going out
byte[] parameters = "{'InfoID':'123456789','Username':'usernametoken','Password':'passwordtoken'}".getBytes("UTF-8");


// Start doing stuff
DataOutputStream os = new DataOutputStream(connection.getOutputStream());
os.write(parameters);
os.close();
InputStream response;

// Check for error , if none store response
if(connection.getResponseCode() == 200){response = connection.getInputStream();}
else{response = connection.getErrorStream();}

InputStreamReader isr = new InputStreamReader(response);
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(isr);
String read = br.readLine();

while(read != null){
sb.append(read);
read = br.readLine();
}
// Print the String
System.out.println(sb.toString());

// Creat JSON off of String
JSONObject token = new JSONObject(sb.toString());

// print JSON
System.out.println("Tokener: " + token.toString());
response.close();

} catch(IOException exc) {
System.out.println("There was an error creating the HTTP Call: " + exc.toString());
}

我得到的回复是这种形式......

{"d":["Sample1","Sample2","Sample3","BD","CN","Name","###","Sample1","Sample2","Sample3","BD","CN","Name","###","Sample1","Sample2","Sample3","BD","CN","Name","###"]}

我想知道是否有更好的方法来发送响应,使得 JSON 看起来像这样:

{"1":["Sample1","Sample2","Sample3","BD","CN","Name","###"],"2":["Sample1","Sample2","Sample3","BD","CN","Name","###"],"3":["Sample1","Sample2","Sample3","BD","CN","Name","###"],"4":["Sample1","Sample2","Sample3","BD","CN","Name","###"]}

最佳答案

好的,我想我在这里看到了你的问题。您希望将数据序列化为

{"1":["Sample1","Sample2","Sample3","BD","CN","Name","###"],"2":["Sample1","Sample2","Sample3","BD","CN","Name","###"],"3":["Sample1","Sample2","Sample3","BD","CN","Name","###"] ... etc

然而,您正在序列化的数据结构是单个链表,这就是它被序列化为单个长列表的原因。您需要做的是更改数据结构。 Dictionary 是完美的,因为它很容易序列化为 JSON。

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public static Dictionary<int,LinkedList<string>> GetInfo(string InfoID, string Username, string Password)
{
var Result = new Dictionary<int,LinkedList<string>>();
try
{
// Do some stuff I can't show you to get the information...

foreach (Result from data operations)
{
var newList = new LinkedList<string>();
newList.AddLast(sample1);
newList.AddLast(sample2);
newList.AddLast(sample3);
newList.AddLast(BD));
newList.AddLast(CN);
newList.AddLast(Name);
newList.AddLast("###");
int number = something //the number before the list
Result.add( number, newList);
}
}catch(Exception exc)
{
.
.
.
}
return Result;
}

关于c# - 使用 JSON 响应从 Java 调用 C# Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18990488/

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