Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JSON响应从Java调用c#webservice_C#_Java_Json_Web Services - Fatal编程技术网

使用JSON响应从Java调用c#webservice

使用JSON响应从Java调用c#webservice,c#,java,json,web-services,C#,Java,Json,Web Services,在我们工作的编程环境中,我们有Java和C#开发者。我有一个用C#创建的web服务,Java开发人员正试图使用它。我一直在编写Java来使用这个web服务,当我得到一个json结果时,它的格式是错误的 以下是我在c#侧的内容: 我得到的答复是这样的 {"d":["Sample1","Sample2","Sample3","BD","CN","Name","###","Sample1","Sample2","Sample3","BD","CN","Name","###","Sample1","Sa

在我们工作的编程环境中,我们有Java和C#开发者。我有一个用C#创建的web服务,Java开发人员正试图使用它。我一直在编写Java来使用这个web服务,当我得到一个json结果时,它的格式是错误的

以下是我在c#侧的内容:

我得到的答复是这样的

{"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
然而,您正在序列化的数据结构是一个单链表,这就是为什么它被序列化为一个长链表的原因。您需要做的是更改数据结构。一个
字典
将是完美的,因为它可以很容易地序列化为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;
}
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
[网络方法]
公共静态字典GetInfo(字符串信息ID、字符串用户名、字符串密码)
{
var Result=newdictionary();
尝试
{
//做一些我不能展示给你的东西来获取信息。。。
foreach(数据操作的结果)
{
var newList=newlinkedlist();
newList.AddLast(样本1);
newList.AddLast(样本2);
newList.AddLast(样本3);
AddLast(BD));
newList.AddLast(CN);
newList.AddLast(名称);
newList.AddLast(“####”);
int number=某物//列表前的数字
结果。添加(编号,新列表);
}
}捕获(异常exc)
{
.
.
.
}            
返回结果;
}

您是否确认web服务工作正常?你试过用C#call来调用它吗?我不确定你想如何得到第二个结果-你只发送了一个带有“Sample1”、“Sample2”等的列表。。。为什么应该有四个呢?
d
是.net的一个安全特性,可以防止json被评估为javascript:您的C#代码实际上是否在一个运行4次的循环中,可能是ID为1、2、3、4?因为否则,我不知道你为什么要得到你现在的数据,或者想要第二种形式的数据。为什么列表中只有一堆
字符串
s,而不是
数据契约
中的字符串,并带有命名属性?我认为这更容易处理。结果实际上是添加到foreach循环中的。我不得不把它排除在外,因为里面有一些敏感数据。不过,我将发布一个编辑后的示例版本。Web服务本身工作正常。我只是想知道如何发回将被视为多个数组的JSON。如果我需要创建一些其他系统来存储结果并将其传回,那就可以了:)。我现在就要编辑这篇文章了。太棒了,它满足了我的需要。我只是用c#编写了一段时间,并不知道这件事!非常感谢@宇宙代码-很高兴能帮上忙:)
{"1":["Sample1","Sample2","Sample3","BD","CN","Name","###"],"2":["Sample1","Sample2","Sample3","BD","CN","Name","###"],"3":["Sample1","Sample2","Sample3","BD","CN","Name","###"] ... etc
[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;
}