简体   繁体   English

在Android上从json解码URL

[英]Decoding url from json on Android

From a Rest API, I get json data in the following format: 从Rest API,我以以下格式获取json数据:

[
    {
        "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. 从API中,我得到Json作为字符串,并且它在url的正斜杠前面包含反斜杠,这是根据json编码规范进行的。 And I am correctly able to json_decode and get url from php. 而且我正确地能够json_decode并从php获取url。 In android I store the json string in a variable named "menu_json". 在android中,我将json字符串存储在名为“ 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. 当json_decode PHP中的API响应时,我得到了正确解码的URL。 But in Android, I am not getting the correctly decoded url in image field. 但是在Android中,我没有在图像字段中得到正确解码的URL。

Please help! 请帮忙!

Here is the complete API response: 这是完整的API响应:

{"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":[]}]}

I'm not sure what Json library you're using, but it looks like org.json . 我不确定您使用的是哪个Json库,但它看起来像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"} 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”:“咖啡”,“ price”:“ 10”,“ veg”:“ 0”,“ id “:” 2“,”类别“:”饮料“}

To parse JSON Array : 要解析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 输出: http : //192.168.1.3/CI/images/tea.jpg

And to display this image to imageview use library : 并显示此图像到imageview使用库:

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

using Loadimagview method of library load image in imageview 在库中使用Loadimagview方法在imageview中加载图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM