2013-09-24 2 views
0

우리의 프로그래밍 환경에는 Java와 C# 개발자가 있습니다. Java 개발자가 사용하려고하는 C#에서 만든 웹 서비스가 있습니다. 이 웹 서비스를 사용하기 위해 Java를 작성했으며 json 결과를 얻는 동안 잘못된 형식입니다.JSON 응답으로 Java에서 C# webservice 호출

[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; 
} 

다음이 자바 쪽 : 여기

내가 C#을 측면에있는 것입니다

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","###"]} 
+0

웹 서비스가 올바르게 작동하는지 확인하셨습니까? 예를 들어 C# 호출을 사용하여 호출 해 보았습니까? –

+0

"Sample1", "Sample2"등의 단일 목록 만 보내는 두 번째 결과에 어떻게 도달할지 잘 모르겠습니다. 왜 그 중 4 개가 있어야합니까? – millimoose

+0

'd'는 json이 javascript로 평가되는 것을 막기 위해 .net의 보안 기능입니다 : http://stackoverflow.com/questions/6588589/why-do-asp-net-json-web-services-return- the-result-in-d –

답변

1

제 생각에는 여기에 문제가 있다고 생각합니다. 당신은 당신의 데이터가

{"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; 
} 
+0

신난다 이것은 내가 필요로하는 것을 위해 일했다. 나는 잠시 동안 C#에서 코딩 만하고 이것에 대해 몰랐다! 고마워요! –

+0

@CodeTheUniverse - 도와 줘서 다행 :) –