Java 从asmx解析json时的问题

Java 从asmx解析json时的问题,java,android,json,parsing,asmx,Java,Android,Json,Parsing,Asmx,我试图解析通过IIS从asmx Web服务接收的json字符串。我收到的字符串如下所示: "{\"Name\":\"Waqas Aslam\",\"Company\":\"ABC Systems AB\",\"Address\":\"myStreet 4\",\"Phone\":\"123456\",\"Country\":\"Sweden\"}" 问题是,我可以成功地检索响应字符串,但无法解析它。这是我的密码: try{ InputStream source = getJso

我试图解析通过IIS从asmx Web服务接收的json字符串。我收到的字符串如下所示:

"{\"Name\":\"Waqas Aslam\",\"Company\":\"ABC Systems AB\",\"Address\":\"myStreet 4\",\"Phone\":\"123456\",\"Country\":\"Sweden\"}"
问题是,我可以成功地检索响应字符串,但无法解析它。这是我的密码:

try{
        InputStream source = getJson(URL);
        String s = streamToString(source);

        Log.i(TAG, s);


        GsonBuilder gsonb = new GsonBuilder();
        Gson gson = gsonb.create();

        JSONObject j = new JSONObject(s);
        Employee em = gson.fromJson(j.toString(), Employee.class);
        lblResult.setText(em.Company);          
}
catch (Exception e) {
    Log.e(TAG, e.toString());
    }
下面是streamToString的方法:

public static String streamToString(InputStream is) {
   //as per 64K size
       BufferedReader reader = new BufferedReader(new InputStreamReader(is), 65728);
       StringBuilder sb = new StringBuilder();

       String line = null;
       try {
           while ((line = reader.readLine()) != null) {
               sb.append(line);
           }
       }
       catch (IOException e) { e.printStackTrace(); }
       finally {
           try { is.close(); }
           catch (Exception e) { e.printStackTrace(); }
       }

       return sb.toString();
   }
下面是班级员工:

import com.google.gson.annotations.SerializedName;

    public class Employee {

            @SerializedName("Name")
            public String Name;

            @SerializedName("Company")
            public String Company;

            @SerializedName("Address")
            public String Address;

            @SerializedName("Phone")
            public String Phone;

            @SerializedName("Country")
            public String Country;
    }
以下是我得到的一个例外:

02-02 10:08:28.877: E/TestJSON(3223): org.json.JSONException: Value {"Name":"Waqas Aslam","Company":"ABC Systems AB","Address":"myStreet 4","Phone":"123456","Country":"Sweden"} of type java.lang.String cannot be converted to JSONObject
如果我手动(通过代码)向JSONObject提供json字符串,那么它可以正常工作,但与我从服务器收到的字符串不兼容。服务器端是否有需要更改的内容?
如果您愿意,您可以尝试使用
HttpPost
从此URL
http://test1.phoniro.se/AndroidTestWebService/Service.asmx/TestJSON2

问题是,您的JSON字符串无效删除所有转义字符(即
\“
),并尝试将其更改为以下内容:

{
    "Name": "WaqasAslam",
    "Company": "ABCSystemsAB",
    "Address": "myStreet4",
    "Phone": "123456",
    "Country": "Sweden"
}

为了回答您的问题,请修改您的服务器端以生成有效的JSON。我上面的示例是完全有效的JSON,您在Gson解析它时不应该有任何问题。当您不确定时,您可以使用一个网站来验证JSON输出。

但问题是,如果我在JQuery中测试从服务器接收的JSON字符串,它工作正常。JQuery省略/忽略转义字符本身?@Waqas我不能肯定JQuery是否正确处理转义字符,但我可以告诉你Gson没有。hmmm,无论如何,谢谢。实际上,我在几个小时前通过修改服务器端已经让它工作了,你的回答证实了这是我收到的Json字符串的问题