无法从json webservice获取数据

时间:2013-03-03 08:47:49

标签: android json

我想从jason webservice

获取数据

JSON响应是:

{"content":[{"id":"1","asset_id":"62","title":"sample page","alias":"","introtext":"","fulltext":"Some Contents"},{"id":"2","asset_id":"62","title":"sample page2","alias":"","introtext":"","fulltext":"Some Contents"},{"id":"3","asset_id":"62","title":"sample page3","alias":"","introtext":"","fulltext":"Some Contents"}]}

访问Here后 我这样做了:

private void parseData() {
    // Creating JSON Parser instance
    JSONParser jParser = new JSONParser();

    // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(BASE_URL);

    try {
        // Getting Array of Contents
        contents = json.getJSONArray(TAG_CONTENTS);

        // looping through All Contents
        for(int i = 0; i < contents.length(); i++){
            JSONObject c = contents.getJSONObject(i);

            // Storing each json item in variable
            id = c.getString(TAG_ID);
            title = c.getString(TAG_TITLE);      
        }

        textView.setText(id + " " + title);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

现在我得到id = 3并且title = sample page3现在我怎样才能获得前两个值!!?

3 个答案:

答案 0 :(得分:1)

Arshay !!试试这个男人!!

private void parseData() {
    // Creating JSON Parser instance
    MyJSONParser jParser = new MyJSONParser();

    // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(BASE_URL);

    try {
        // Getting Array of Contents
        jsonArrar = json.getJSONArray(TAG_CONTENTS);
        List list = new ArrayList<String>();
        // looping through All Contents
        for(int i = 0; i < jsonArrar.length(); i++){
           // JSONObject c = jsonArrar.getJSONObject(i);


            String id1=jsonArrar.getJSONObject(i).getString(TAG_ID);                
            String title=jsonArrar.getJSONObject(i).getString(TAG_TITLE);               
            String fullText=jsonArrar.getJSONObject(i).getString(TAG_FULL_TEXT);                

            list.add(id1);
            list.add(title);
            list.add(fullText);
        }

        Iterator<String> iterator = list.iterator();
        StringBuilder builder = new StringBuilder();
        while (iterator.hasNext()) {
            String string = iterator.next();                
            builder.append(string+"\n");

        }

        textView.setText(builder);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

答案 1 :(得分:0)

您的行是JSONObject而不是JSONArray

你应该这样使用它:

JSONObject jso = new JSONObject(line);
JSONArray jsa = new JSONArray(jso.getJSONArray("content"));

答案 2 :(得分:0)

尝试这样的事情

// jsonData:response

列表与LT;串GT; contents = new ArrayList&lt;串GT;();

String [] val;

            try {


        JSONObject jsonObj = new JSONObject(jsonData);
        if (jsonObj.get(JSON_ROOT_KEY) instanceof JSONArray) {

            JSONArray array = jsonObj.optJSONArray(JSON_ROOT_KEY);
            for (int loop = 0; loop < array.length(); loop++) {
val = new String[loop];
JSONObject Jsonval = array.getJSONObject(loop);
val.Jsonval.getString(TAG_ID);
val.Jsonval.getString(asset_id);

。 。 等

contents.add(VAL);

}
}

}

相关问题