【问题标题】:How to parse JSON .net web service to Android properly?如何正确地将 JSON .net Web 服务解析为 Android?
【发布时间】:2012-06-09 16:06:52
【问题描述】:

我已经尝试了许多版本的解析来自我的 .net Web 服务和我当前的 JSON 数据的版本 在我的 JSON 数据前面添加一个 {"d": ..... 我不知道为什么。

手动调用我的网络服务时,数据格式正确。 但是,当我使用这个解析器时,我检查了使用 .names() 以列出 JSONObjects 的名称,它只显示 ["d"]

有没有推荐的代码来解析java中的JSON webservices?

这是我解析数据的代码

        //initialize
                InputStream is = null;
                String result = "";
                JSONObject jArray = null;
                String url = "http://10.0.2.2:1672/Eventurous/WsEventurousMobile.asmx/getEventsList";
                //http post
                try{

                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost(url);

                    JSONObject obj = new JSONObject();
                    obj.put("category",category);
                    httppost.setEntity(new StringEntity(obj.toString(),"UTF-8"));

                    httppost.setHeader("Accept","application/json");
                    httppost.setHeader("Content-type","application/json");

                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();

                }catch(Exception e){
                    Log.e("log_tag", "Error in http connection "+e.toString());
                }

                //convert response to string
                try{
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    is.close();
                    result=sb.toString();
                }catch(Exception e){
                    Log.e("log_tag", "Error converting result "+e.toString());
                }

                //try parse the string to a JSON object
                try{
                        jArray = new JSONObject(result);
                }catch(JSONException e){
                    Log.e("log_tag", "Error parsing data "+e.toString());
                }

                try{

                        JSONArray catalogObj = jArray.getJSONArray("Table");
   //Im using the newtonsoft json dataset converter and so the name of the JSONArray
       //is by default Table and i have no idea how to change it either

                        JSONObject event = catalogObj.getJSONObject(0);
                        return event.getString("EventName");        

                }
                catch(Exception e)
                {
                    return e.toString();
                }

【问题讨论】:

    标签: android asp.net json web-services


    【解决方案1】:

    我建议使用 gson 库,http://code.google.com/p/google-gson/

    有没有推荐的代码来解析java中的JSON webservices?

    在不知道 json 数据的情况下,很难给出具体的建议。但无论如何,我建议使用在 Android 开发人员中非常流行的 gson 库。

    【讨论】:

    • d 变量显然是 .net 的一项安全功能,我通过获取 d 变量中的数据来解决问题,将其重新解析为 JSONObject 的 Web 服务的“正确”响应。我做了什么 Object JSONdata = jArray.get("d"); jArray.getJSONArray("表"); JSONObject 目录 = new JSONObject(JSONdata.toString()); JSONArray catalogObj=catalog.getJSONArray("Table"); JSONObject 事件 = catalogObj.getJSONObject(0);
    • 无论如何感谢您的回复。我会记住的。现在我认为我不需要它,因为它可以工作。
    • 好的,我明白了。但是这个问题并不是真正的 Android 特定的,也不是客户端特定的,因为它只是一个服务器端问题,而返回的 json 实际上是有效的;建议删除Android标签。 “手动调用我的网络服务时,数据格式正确。”您的意思是服务器返回不同的 json 响应吗?如何手动调用 Web 服务?
    • 没关系,经过几次 Bing 搜索后,就像 Cory Tam 提到的,这是 .Net Web 服务的正常行为。为了处理这个问题并获得正确的 Json 字符串,我做了这个 JSONObject jsonObject = (JSONObject) new JSONTokener(httpResponse).nextValue();字符串 jsonString = jsonObject.getString("d");希望这对使用返回 Json 的 Asp.Net Web 服务的人有所帮助。
    猜你喜欢
    • 2010-10-30
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 2015-01-24
    • 1970-01-01
    • 2011-08-13
    相关资源
    最近更新 更多