Need help with Strings

Discussion in 'Plugin Development' started by skipperguy12, May 20, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    skipperguy12

    Okay, so I need to get only a specific part of a String, but I need to blank out a small amount of it.

    Here's what I need to cut apart:
    Code:
    [{"broadcast_part": 1, "featured": true, "channel_subscription": false, "audio_codec": "uncompressed", "id": "5708525952", "category": "gaming", "title": "\u4e9e\u6d32\u7d71\u795e \u54ad\u54ad\u4e09\u6bd4\u9748 -  \u9738\u6c23\u56de\u6b78  Gragas God :: TW rank Diamond", "geo": "TW", "video_height": 960, "site_count": 4509, "embed_enabled": false, "channel": {"subcategory": null, "producer": true, "image_url_huge": "http://www-cdn.jtvnw.net/static/images/404_user_600x600.png", "timezone": "", "screen_cap_url_huge": "http://static-cdn.jtvnw.net/previews/live_user_asiagodtonegg3be0-630x473.jpg", "id": "5708525952", "views_count": 1428767, "category": "gaming", "embed_code": "<object type=\"application/x-shockwave-flash\" data=\"http://www.justin.tv/widgets/live_embed_player.swf?channel=asiagodtonegg3be0\" id=\"live_embed_player_flash\" height=\"300\" width=\"400\" bgcolor=\"#000000\"><param name=\"allowFullScreen\" value=\"true\"/><param name=\"allowScriptAccess\" value=\"always\" /><param name=\"allowNetworking\" value=\"all\" /><param name=\"movie\" value=\"http://www.justin.tv/widgets/live_embed_player.swf\" /><param name=\"flashvars\" value=\"hostname=www.justin.tv&channel=asiagodtonegg3be0&auto_play=false&start_volume=25\" /></object><a href=\"http://www.justin.tv/asiagodtonegg3be0#r=-rid-&amp;s=em\" class=\"trk\" style=\"padding:2px 0px 4px; display:block; width:345px; font-weight:normal; font-size:10px; text-decoration:underline; text-align:center\">Watch live video from asiagodtonegg3be0 on www.justin.tv</a>", "title": "asiagodtonegg3be0", "image_url_tiny": "http://www-cdn.jtvnw.net/static/images/404_user_50x50.png", "screen_cap_url_large": "http://static-cdn.jtvnw.net/previews/live_user_asiagodtonegg3be0-320x240.jpg", "channel_url": "http://www.justin.tv/asiagodtonegg3be0", "status": "\u4e9e\u6d32\u7d71\u795e \u54ad\u54ad\u4e09\u6bd4\u9748 -  \u9738\u6c23\u56de\u6b78  Gragas God :: TW rank Diamond", "meta_game": "League of Legends", "tags": null, "image_url_small": "http://www-cdn.jtvnw.net/static/images/404_user_70x70.png", "screen_cap_url_medium": "http://static-cdn.jtvnw.net/previews/live_user_asiagodtonegg3be0-150x113.jpg", "language": "en", "embed_enabled": false, "subcategory_title": "", "image_url_medium": "http://www-cdn.jtvnw.net/static/images/404_user_150x150.png", "image_url_large": "http://www-cdn.jtvnw.net/static/images/404_user_300x300.png", "mature": null, "screen_cap_url_small": "http://static-cdn.jtvnw.net/previews/live_user_asiagodtonegg3be0-70x53.jpg", "login": "asiagodtonegg3be0", "category_title": "Gaming"}, "up_time": "Mon May 20 03:21:34 2013", "meta_game": "League of Legends", "format": "live", "channel_count": 6142, "embed_count": 1176, "abuse_reported": false, "video_width": 1280, "stream_type": "live", "name": "live_user_asiagodtonegg3be0", "language": "en", "stream_count": 5685, "video_bitrate": 261.5703125, "broadcaster": "obs", "video_codec": "AVC", "channel_view_count": 1432775}]
    Now, that's twitch's api. I need to get a tiny part of it:
    Code:
     "channel_count": 6142
    So I thought, okay, they've got these commas, so i'll split them into a String array, and tried doing this:
    Code:
            String[] arrayData = data.dataToString().split(",");
            String channel_count = arrayData[41];
            channel_count = channel_count.replaceAll("\"channel_count\":", "");
            viewers = Integer.valueOf(channel_count);
    Turns out, it's not always 41st in the data, it changes the list size, and very fast. Any ideas?
     
  2. Offline

    Tirelessly

    skipperguy12

    You could try using indexes, but one way you could make it so that you could get any data:

    Code:
    Map<String, String> dataMap = new HashMap<String, String>();
    String[] split = data.dataToString().split(", ");
    for(String info: split){
      String[] infoSplit = info.split(": ");
      dataMap.put(infoSplit[0], infoSplit[1]);
    }
    
    Then to get it:

    Code:
    String channel_count = dataMap.get("\"channel_count\"");
    
     
  3. Offline

    itunes89

    Regex FTW: channel_count":\s+?\d+
     
Thread Status:
Not open for further replies.

Share This Page