简体   繁体   English

如何解析我的json结果

[英]How can parse my json result

I am working in android. 我在android中工作。 I want to parse my json data. 我想解析我的json数据。

This is my json data:- 这是我的json数据:-

{ {

"response":{ “响应”:{

  "groups":[

     {

        "type":"nearby",
        "name":"Nearby",
        "items":[
           {
              "id":"4ed0c8f48231b9ef88fe5f09",
              "name":"Banayan Tree School",
              "contact":{

              },
              "location":{
                 "lat":26.857954980225713,
                 "lng":75.76602927296061,
                 "distance":510
              },
              "categories":[
                 {
                    "id":"4bf58dd8d48988d1a8941735",
                    "name":"General College & University",
                    "pluralName":"General Colleges & Universities",
                    "shortName":"Other - Education",
                    "icon":"https:\/\/foursquare.com\/img\/categories\/education\/default.png",
                    "parents":[
                       "Colleges & Universities"
                    ],
                    "primary":true
                 }
              ],
              "verified":false,
              "stats":{
                 "checkinsCount":5,
                 "usersCount":4,
                 "tipCount":0
              },
              "hereNow":{
                 "count":0
              }
           }

        ]
     }
  ]

} } }}

i want to use icon to show icon in imageview. 我想用图标来显示ImageView的图标 please suggest me how can i get this icon value and how can i user this icon url in imageview. 请建议我如何获取此图标值以及如何在imageview中使用此图标url。

Thank you in advance. 先感谢您。

i am trying this, but still it is creating error:- this is my code:- but still is creating error:- 我正在尝试此操作,但仍在创建错误:-这是我的代码:-但仍在创建错误:-

JSONArray groups= (JSONArray) jsonObj.getJSONObject("response").getJSONArray("groups"); JSONArray groups =(JSONArray)jsonObj.getJSONObject(“ response”)。getJSONArray(“ groups”);

int length= groups.length(); int length = groups.length(); if (length > 0){ for (int i = 0; i < length; i++) if(length> 0){for(int i = 0; i <length; i ++)

{ {

 JSONObject group= (JSONObject) groups.get(i); JSONArray items =(JSONArray) group.getJSONArray("items"); 

for (int j = 0; j < items.length(); j++) for(int j = 0; j <items.length(); j ++)

{ {

JSONObject item = (JSONObject) items.get(j); JSONObject item =(JSONObject)items.get(j);

JSONObject iconobject=(JSONObject) item.getJSONObject("categories");/ /this is creating error that JSON.typeMismatch JSONObject iconobject =(JSONObject)item.getJSONObject(“ categories”); / /这正在创建JSON.typeMismatch的错误

venue.icon=iconobject.getString("icon"); place.icon = iconobject.getString(“ icon”);

}}}} }}}}

The class JSONObject can help you: JSONObject类可以帮助您:

String data = ... // your json data
JSONObject json = new JSONObject(data);

You can access nodes in your structure with help of getJSONObject(String) and getJSONArray(String) . 您可以在getJSONObject(String)getJSONArray(String)帮助下访问结构中的节点。

For example: 例如:

JSONObject response = json.getJSONObject("response");
JSONArray groups = response.getJSONArray("groups");
JSONObject firstGroup = groups.getJSONObject(0);
// and so on

When you got your node that contains your icon value you can use the getString(String) method to get the icon url: 当您的节点包含图标值时,可以使用getString(String)方法获取图标url:

JSONObject firstCategory = categories.getJSONObject(0);
String iconUrl = firstCategory.getString("icon");

After you got the url you have to download the image before you can use it. 收到网址后,您必须先下载图像,然后才能使用它。 How to download an image from an url is described here 此处介绍如何从网址下载图片

When you downloaded the image you can update the imageview: 下载图像后,可以更新imageview:

Bitmap image = loadBitmap(iconUrl); // how to implement loadBitmap is shown in the link above
ImageView iv = findViewById(R.id.my_imageview);
iv.setImageBitamp(image);

Try this: 尝试这个:

try {
    JSONArray jArray = new JSONArray(result);
    // get into the 'groups' array
    JSONObject jData = jArray.getJSONObject(0);
    JSONArray jGroupsArray = jData.getJSONArray("groups");
    // get into the 'items' array
    jData = jArray.getJSONObject(2);
    JSONArray jItemsArray = jData.getJSONArray("items");
    // get into the 'categories' array
    jData = jArray.getJSONObject(4);
    JSONArray jCategoriesArray = jData.getJSONArray("categories");
    // get into the 'icon' value as String and use it as you please
    jData = jArray.getJSONObject(4);
    String iconURL = jData.getString("icon");
} catch (JSONException e) {
    Log.e(Constants.LOG_TAG, "Error parsing data", e);
}

Hope this helps 希望这可以帮助

Refer to the JSON documentation: http://www.json.org/javadoc/org/json/package-summary.html . 请参阅JSON文档: http : //www.json.org/javadoc/org/json/package-summary.html It's really simple. 真的很简单。

In your case, you would have to read the JSON string to a JSON object, then parse "response" as a JSONObject, "groups" as a JSONArray inside "response", iterate through the JSONObjects contained in the "groups" array, parse "Items" as a JSONArray inside your JSONObject, and son on... 在您的情况下,您必须将JSON字符串读取到JSON对象,然后将“ response”解析为JSONObject,将“ groups”解析为JSONArray,在“ response”中进行迭代,遍历“ groups”数组中包含的JSONObject, “项目”作为JSONObject内的JSONArray,在...上

You should be able to get to the URL in no time. 您应该可以立即访问该URL。

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

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