繁体   English   中英

在Android上从json解码URL

[英]Decoding url from json on Android

从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"
    }
]

从API中,我得到Json作为字符串,并且它在url的正斜杠前面包含反斜杠,这是根据json编码规范进行的。 而且我正确地能够json_decode并从php获取url。 在android中,我将json字符串存储在名为“ menu_json”的变量中。

然后,我尝试使用以下代码解析并从中获取图像网址:

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);
}

我得到的输出是:

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

图片字段应为:

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

但是我得到的只是:

tea.jpg

当json_decode PHP中的API响应时,我得到了正确解码的URL。 但是在Android中,我没有在图像字段中得到正确解码的URL。

请帮忙!

这是完整的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":[]}]}

我不确定您使用的是哪个Json库,但它看起来像org.json 我以为您的代码看起来不错,所以我实现了它,但是看不到您看到的输出。 我的猜测是您的输入数据不是您期望的那样。

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);
}

输出:

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“,”类别“:”饮料“}

要解析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);
     }

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

并显示此图像到imageview使用库:

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

在库中使用Loadimagview方法在imageview中加载图像

暂无
暂无

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

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