无法使用url检索android中的所有json值

时间:2014-06-30 04:50:35

标签: android json arrays jsonobject

我正在尝试使用android中的url检索json数据。我的json数组如下。我使用了下面的编码,但问题是我将length的{​​{1}}等于1,尽管数组中有更多的值。其次我得到jarray的长度等于15,但相同的值显示15次,即id = 1显示15次。我已经发布了代码,因为我对json很新,请一步一步地告诉我我哪里出错了。

newarr

Json解析代码

[
{
    "items": [
        {
            "id": "11",
            "Item_Id": "123",
            "Item_Name": "Chicken Cream Soup",
            "Price": "8",
            "Currency": "AED",
            "Category": "Soup",
            "Description": "Creamy Chicken Soup with garnish & side helpings",
            "Unit": "2",
            "food_type": "Non",
            "Image_Large": "/images_large/chickensoup.jpg",
            "Image_Thumb": "/images_large/chickensoup.jpg",
            "Timestamp": "6/23/2014 9:49:43 PM",
            "Promotion": "",
            "Item_Name_arabic": "حساء الطماطم",
            "Item_Name_russian": "",
            "Currency_arabic": "درهم",
            "Currency_russian": "",
            "Description_arabic": "حساء الطماطم",
            "Description_russian": "",
            "Note": "",
            "Nutritional_info": "",
            "extrafield_1": "",
            "extrafield_2": "",
            "preferncess": [
                "No Salt",
                "Extra Sugar"
            ],
            "preferncess_ids": [
                "1",
                "2"
            ],
            "price": [
                "4",
                "5"
            ],
            "preferncess_arabic": [
                "لا الملح",
                "سكر اضافية"
            ]
        },
        {
            "id": "12",
            "Item_Id": "501",
            "Item_Name": "Pasta Napolitan",
            "Price": "18",
            "Currency": "AED",
            "Category": "Pasta",
            "Description": "Pasta in Napolitan Sauce",
            "Unit": "20",
            "food_type": "Non",
            "Image_Large": "/images_large/pasta.jpg",
            "Image_Thumb": "/images_large/pasta.jpg",
            "Timestamp": "6/23/2014 9:47:45 PM",
            "Promotion": "",
            "Item_Name_arabic": "حساء الطماطم",
            "Item_Name_russian": "",
            "Currency_arabic": "درهم",
            "Currency_russian": "",
            "Description_arabic": "حساء الطماطم",
            "Description_russian": "",
            "Note": "",
            "Nutritional_info": "",
            "extrafield_1": "",
            "extrafield_2": "",
            "preferncess": [
                "No Salt"
            ],
            "preferncess_ids": [
                "3"
            ],
            "price": [
                "5"
            ],
            "preferncess_arabic": [
                "لا الملح"
            ]
        },
  //some more values till id=25


  "categories": [
        {
            "categoryName": "Salads",
            "categoryShortName": "Salads",
            "catid": "0",
            "categoryArabicName": "سلطة خضراء"
        },
        {
            "categoryName": "Mezzah",
            "categoryShortName": "Mezzah",
            "catid": "1",
            "categoryArabicName": "المزة"
        },
    //some more values till id=25

   "questions": [
        {
            "q_id": "1",
            "q_question": "How would you rate our Menu ?",
            "q_option1": "Excellent",
            "q_option2": "Very Good",
            "q_option3": "Good",
            "q_option4": "Bad",
            "q_option5": "Terrible",
            "Timestamp": "9/12/2013 3:31:55 PM",
            "q_status": "1"
        },
        {
            "q_id": "2",
            "q_question": "How would you rate our presentation, taste and quality of food ?",
            "q_option1": "Excellent",
            "q_option2": "Very Good",
            "q_option3": "Good",
            "q_option4": "Bad",
            "q_option5": "Terrible",
            "Timestamp": "9/12/2013 3:31:55 PM",
            "q_status": "1"
        },
  //some more values here

1 个答案:

答案 0 :(得分:0)

试试这种方式

public String postData(String url, List<NameValuePair> nameValuePairs)
        throws Exception {
    // Getting the response handler for handling the post response
    ResponseHandler<String> res = new BasicResponseHandler();
    HttpPost postMethod = new HttpPost(url);

    // Setting the data that is to be sent
    postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    // Execute HTTP Post Request
    String response = httpClient.execute(postMethod, res);
Log.i(TAG , "response :: "+response);
    return response;
}

对于json。

rootObject = new JSONObject(postData(url,nameValuesPair));