0

From a Rest API, I get json data in the following format:

[
    {
        "id": "1",
        "item": "tea",
        "price": "7.5",
        "image": "http:\/\/192.168.1.3\/CI\/images\/tea.jpg",
        "veg": "0",
        "category": "drinks"
    },
    {
        "id": "2",
        "item": "coffee",
        "price": "10",
        "image": "http:\/\/192.168.1.3\/CI\/images\/coffee.jpg",
        "veg": "0",
        "category": "drinks"
    }
]

From the API I get Json as a string and it contains backslashes in front of url's forward slashes, which is according to the json encoding specification. And I am correctly able to json_decode and get url from php. In android I store the json string in a variable named "menu_json".

Then I am trying to parse and get the image url from it using the following code:

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
try{
    JSONObject menuApiObj = new JSONObject(menu_json);
    JSONArray menuObj = menuApiObj.getJSONArray("menu");
    for (int i = 0; i < menuObj.length(); i++){
        JSONObject row = menuObj.getJSONObject(i);
        rowString = row.getString("image");
        imageUrl = row.toString();
        Log.e("rowString", rowString);
        Log.e("imageUrl", imageUrl);
}

The output I get is:

{
    "id": "1",
    "item": "tea",
    "price": "7.5",
    "image": "tea.jpg",
    "veg": "0",
    "category": "drinks"
}

The image field is supposed to be:

http://192.168.1.3/CI/images/tea.jpg

But instead I get just:

tea.jpg

When json_decode the API response in PHP, I get the correctly decoded url. But in Android, I am not getting the correctly decoded url in image field.

Please help!

Here is the complete API response:

{"menu":[{"id":"1","item":"tea","price":"7.5","image":"tea.jpg","veg":"0","category":"drinks"},{"id":"2","item":"cofee","price":"10","image":"coffee.jpg","veg":"0","category":"drinks"},{"id":"3","item":"crispy chicken","price":"160","image":"crispy-chicken.jpg","veg":"0","category":"curries"}],"cat_wise":[{"category":"drinks","items":[{"id":"1","item":"tea","price":"7.5","image":"http:\/\/192.168.1.3\/CI\/images\/tea.jpg","veg":"0","category":"drinks"},{"id":"2","item":"cofee","price":"10","image":"http:\/\/192.168.1.3\/CI\/images\/coffee.jpg","veg":"0","category":"drinks"}]},{"category":"curries","items":[{"id":"3","item":"crispy chicken","price":"160","image":"http:\/\/192.168.1.3\/CI\/images\/crispy-chicken.jpg","veg":"0","category":"curries"}]},{"category":"main","items":[]}]}
1
  • Show you complete JSON data. What does menuApiObj.getJSONArray("menu") mean? There is no such array in the given JSON data, Commented May 27, 2016 at 7:07

2 Answers 2

2

I'm not sure what Json library you're using, but it looks like org.json. I thought your code looked sane, so I implemented it and do not see the output that you are seeing. My guess is that your input data isn't what you expect it to be.

final JSONArray menuObj = new JSONArray("[\n" +
        "    {\n" +
        "        \"id\": \"1\",\n" +
        "        \"item\": \"tea\",\n" +
        "        \"price\": \"7.5\",\n" +
        "        \"image\": \"http://192.168.1.3/CI/images/tea.jpg\",\n" +
        "        \"veg\": \"0\",\n" +
        "        \"category\": \"drinks\"\n" +
        "    },\n" +
        "    {\n" +
        "        \"id\": \"2\",\n" +
        "        \"item\": \"coffee\",\n" +
        "        \"price\": \"10\",\n" +
        "        \"image\": \"http://192.168.1.3/CI/images/coffee.jpg\",\n" +
        "        \"veg\": \"0\",\n" +
        "        \"category\": \"drinks\"\n" +
        "    }\n" +
        "]");
for (int i = 0; i < menuObj.length(); i++){
    final JSONObject row = menuObj.getJSONObject(i);
    System.out.println("imageUrl: " +  row.getString("image"));
    System.out.println("rowString: " +  row);
}

Output:

imageUrl: http://192.168.1.3/CI/images/tea.jpg rowString: {"image":"http://192.168.1.3/CI/images/tea.jpg","item":"tea","price":"7.5","veg":"0","id":"1","category":"drinks"} imageUrl: http://192.168.1.3/CI/images/coffee.jpg rowString: {"image":"http://192.168.1.3/CI/images/coffee.jpg","item":"coffee","price":"10","veg":"0","id":"2","category":"drinks"}

Sign up to request clarification or add additional context in comments.

2 Comments

Yes I am using org.json
Oh, I was getting frustrated and took a closer look at the API response. Looks like "menu" in json doesn't contain the full URL, only the "cat_wise" menu contains the full image URL. Your help is much appreciated. Thank you so much.
0

To parse JSON Array :

    JSONArray jArray = jObject.getJSONArray("menu");

     for (int i = 0; i < jArray.length(); i++) {
       JSONObject innerjObject =jArray.getJSONObject(i);
       String image =innerjObject.getString("image");
       array_list.add(image);
     }

output: http://192.168.1.3/CI/images/tea.jpg

And to display this image to imageview use library :

compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'

using Loadimagview method of library load image in imageview

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.