【问题标题】:Trouble parsing JSON from android because the asp.net server doesn't return array names无法从 android 解析 JSON,因为 asp.net 服务器不返回数组名称
【发布时间】:2013-03-13 09:50:16
【问题描述】:

我制作了一个小型 MVC4 应用程序,它以 API 的形式从 SQL 服务器返回数据,本地 url 是 http://10.0.0.116:35577/api/articoli(我已经启用了对本地 IIS 实例的网络访问,这实际上让我停止了一整天,直到我想通了),它返回的格式如下:

[{"id":0,"desc":"Pizza"},{"id":1,"desc":"Pasta"},{"id":2,"desc":"Science"},{"id":3,"desc":"spaghetti"},{"id":4,"desc":"sfas"},{"id":5,"desc":"test"}]

调用http://10.0.0.116:35577/api/articoli/1 返回单个元素:

{"id":1,"desc":"Pasta"}

我在从我的 android 应用程序中解析它时遇到了问题,从我收集到的数组应该有一个标题名称或其他东西,这里完全没有,我真的不知道如何在这里添加它,或者如果我真的需要一个。

据我所知,我从教程中获得的解析器类不适合这种数据,我真的不知道如何使它与这样的结果一起工作。

解析器如下:

public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

public JSONObject getJSONFromUrl(String url) {

    // Making HTTP request
    try {

        URL aaa = new URL(url);
        URLConnection uConnection = aaa.openConnection();
        uConnection.connect();


        //is = httpEntity.getContent();
        is = aaa.openStream();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

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

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

    // return JSON String
    return jObj;

}
}

我不得不编辑解析器的 url 检索部分,因为它试图通过使用 POST 来获取数据,服务器应用程序将其解释为尝试插入数据并调用了错误的方法,

这是一段调用解析器的代码,它应该检索每个元素,然后将每个元素作为一行放在列表视图中,然后单击它应该调用一个读取单个元素的活动:

private void fillDataJSON() {
        JSONParser jParser = new JSONParser();

        JSONObject json = jParser.getJSONFromUrl(URL);
        JSONArray articoli;
        try {
            articoli = json.getJSONArray("Articoli");

            if (articoli == null) {
                throw new NullPointerException();
            }

            for(int i = 0; i< articoli.length(); i++) {
                JSONObject c = articoli.getJSONObject(i);

                String id = c.getString(KEY_ID);
                String desc = c.getString(KEY_DESC);

                HashMap<String, String> map = new HashMap<String, String>();

                map.put(KEY_ID, id);
                map.put(KEY_DESC, desc);

                menuItems.add(map);

                adapter = new SimpleAdapter(ApiTest.this, menuItems,
                        R.layout.activity_api_test_row,
                        new String[] { KEY_DESC },
                        new int[] { R.id.TextView01 });
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

我得到的错误如下:

03-13 10:05:33.291: E/JSON Parser(11811): Error parsing data org.json.JSONException: Value [{"id":0,"desc":"Pizza"},{"id":1,"desc":"Pasta"},{"id":2,"desc":"Science"},{"id":3,"desc":"spaghetti"},{"id":4,"desc":"sfas"},{"id":5,"desc":"test"}] of type org.json.JSONArray cannot be converted to JSONObject

这可以在 android 端修复还是我必须更改我的服务器的工作方式?

服务器只是通过控制器返回数据,其他的都交给框架,例如:

 public IEnumerable<Articoli> GetAllArticoli()
    {
        return repository.GetAll();
    }

除此之外不做任何事情

【问题讨论】:

  • 你知道数据什么时候是JSONArray(调用api/articoli时),什么时候是JSONObject(调用api/articoli/1时),需要相应地解析字符串。

标签: android json api asp.net-mvc-4


【解决方案1】:

JSON 文档的根元素可以是 JSONObject 或 JSONArray(或原语之一)。您总是将其解析为对象:

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

当文档的根元素是数组时,您应该将其解析为new JSONArray(json)。您可以使用JSONTokener 安全地执行此操作:

// try parse the string to a JSON object
try {
    JSONTokener jt = new JSONTokener(json);
    Object rootElement = jt.nextValue();
    if (rootElement instanceof JSONObject) {
       // You got an object from the response
    } else if (rootElement instanceof JSONArray) {
       // You got a JSON array
    }
    // else... -> it can also be String, Boolean, Integer, Long or Double
} catch (JSONException e) {
    Log.e("JSON Parser", "Error parsing data " + e.toString());
}

如果您知道自己的要求,那么使用正确的响应类型会更容易。

【讨论】:

  • 打败我好先生!这正是我要写的。他还需要更改方法的返回类型,以及他存储 JSON 的类级别变量的类型
【解决方案2】:

您可以简单地更改fillDataJSON() 中的代码。只需将类型从JSONObject 更改为JSONArray

JSONArray json = jParser.getJSONFromUrl(URL);
        JSONArray articoli;
        try {
            articoli = json;

并在您的JSONParser 中更改为:

// try parse the string to a JSON object
    try {
        jObj = new JSONArray(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

将jObj的类型改为JSONArray

static JSONArray jObj = null;

以及从JSONObjectJSONArray的类的返回类型

public JSONObject getJSONFromUrl(String url) {

【讨论】:

  • 设置断点并调试代码以找到问题。我相信这段代码会 insha Allah 解决您的问题。您的问题现在可能在其他地方?
  • 别担心,我现在的问题是这个问题的另一个问题,感谢您的回答,因为与以前的答案一起,这基本上就是我所做的
  • 很高兴为您提供帮助..您可以从另一个问题中提出一个问题,然后再次指出那个问题。让我们看看我是否能比其他人更快地解决这个问题? :微笑:
  • 别担心,只是列表视图中的每个元素都显示为NULL,我一定是在某个地方搞砸了,但没关系,因为我可以很容易地解决它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-22
  • 1970-01-01
  • 1970-01-01
  • 2013-06-25
  • 2021-08-20
相关资源
最近更新 更多