www.webdeveloper.com
Results 1 to 9 of 9

Thread: parse / Split String

  1. #1
    Join Date
    Apr 2013
    Posts
    7

    parse / Split String

    I have this string

    Note the part I am after can differ in length...so that has to be taken into account..

    {"token":"bgfhf8758dfh48hdhwsu8d.","expires";1408485233476}

    But I only want that part between the " " the red part above

    How can I do that?

  2. #2
    Join Date
    Apr 2013
    Posts
    7
    It will always start on the 11th character but again the length is not always the same....

    So in between the 3rd " and the 4th "

  3. #3
    Join Date
    Mar 2009
    Posts
    590
    Weird. Your "string" seems to be an incorrectly formatted JSON object. (semi-colon after "expires" rather than colon) But if it truly is a string, then you can do this:
    Code:
    //note that your "string" is enclosed in single quotes
    var v = '{"token":"bgfhf8758dfh48hdhwsu8d.","expires";1408485233476}';
    var arr = v.split("\"");
    var result = arr[3];
    console.log(result);
    
    /* running this produces:
    bgfhf8758dfh48hdhwsu8d.
    as output
    */
    Are you absolutely sure that:
    {"token":"bgfhf8758dfh48hdhwsu8d.","expires";1408485233476}
    isn't supposed to represent an object?

    --on Edit--this should provide a more practical solution for you

    Code:
    function doParse(str){  //str should be your string
       var arr = str.split("\"");
       return arr[3];
    }
    Last edited by Tcobb; 08-19-2014 at 10:06 PM.

  4. #4
    Join Date
    Apr 2013
    Posts
    7
    Was able to get this working with JSON but your examples are working as well....

    Where test woudl be this value - {"token":"bgfhf8758dfh48hdhwsu8d.","expires";1408485233476}

    var test = document.getElementById('tokentext').value
    var JsonParseToken = JSON.parse(test);
    alert(JsonParseToken.token);

  5. #5
    Join Date
    Mar 2009
    Posts
    590
    Where test woudl be this value - {"token":"bgfhf8758dfh48hdhwsu8d.","expires";1408485233476}

    var test = document.getElementById('tokentext').value
    var JsonParseToken = JSON.parse(test);
    alert(JsonParseToken.token);
    That confuses me. I'm not sure what you're doing there or trying to accomplish. Maybe this will provide some clarity, and then again maybe not:

    Code:
    //HERE ITS A STRING--note single quotes around it
    var k = '{"token":"bgfhf8758dfh48hdhwsu8d.","expires":1408485233476}'; //semi-colon replaced with colon
    
    k = JSON.parse(k);    //JSON parse should be used BECAUSE its a string
    console.log(k.token);    //prints bgfhf8758dfh48hdhwsu8d.
    console.log(k.expires);  //prints 1408485233476
    
    //HERE ITS NOT A STRING, ITS AN OBJECT IN JSON NOTATION --no quotes around the structure
    var k = {"token":"bgfhf8758dfh48hdhwsu8d.","expires":1408485233476}; //semi-colon replaced with colon
    
    //no need for JSON.parse--its already an object
    console.log(k.token);    //prints bgfhf8758dfh48hdhwsu8d.
    console.log(k.expires);  //prints 1408485233476
    
    //if you're not sure what it currently is at any point, you can always run it through
    
    function makeObj(data){
        if(typeof(data) == 'string'){
    	return JSON.parse(data);
        }
        return data;
    }

  6. #6
    Join Date
    Apr 2013
    Posts
    7
    what I have been trying to do is get the value out of the string

    I need the token value: bgfhf8758dfh48hdhwsu8d.

    1. The Variable below 'Test' has this string in it:
      {"token":"bgfhf8758dfh48hdhwsu8d.","expires";1408485233476}
    2. I was able to use JSON and get the value of Token from above


    Code:
    	
           var test = document.getElementById('tokentext').value
    
    	// PARSE WITH JSON
    	var JsonParseToken = JSON.parse(test);
    
    	var MapServerToken = (JsonParseToken.token);

  7. #7
    Join Date
    Apr 2013
    Posts
    7
    My last post sets the Variable MapServerToken = bgfhf8758dfh48hdhwsu8d.

  8. #8
    Join Date
    Mar 2009
    Posts
    590
    I don't know what browser you're using, but :

    {"token":"bgfhf8758dfh48hdhwsu8d.","expires";1408485233476}
    WON'T parse under Chrome because you have a semi-colon rather than a colon after "expires". Change it.

    And if you're going to use this:
    var test = document.getElementById('tokentext').value
    You need something in your HTML like the following:

    Code:
    <input type="hidden" id="tokentext" value='{"token":"bgfhf8758dfh48hdhwsu8d.","expires":1408485233476}'>

  9. #9
    Join Date
    Apr 2013
    Posts
    7
    Just so you know this is working right now....

    The session value "tokentext" = {"token":"bgfhf8758dfh48hdhwsu8d.","expires":1408485233476}
    As such the In hidden id = ""tokentext = {"token":"bgfhf8758dfh48hdhwsu8d.","expires":1408485233476}
    I am grabbing the document.getElementById('tokentext').value with var test in JS and parsing with Json

    IN .ASPX:

    <form id="form1" runat="server">
    <div>
    <input type="hidden" id="tokentext" name="tokentext" runat="server"/>
    </div>
    </form>

    IN .ASPX.JS

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    tokentext.Value = Session("tokentext")
    End Sub

    IN JavaScript

    // GET TOKEN AND ASSIGN TO VARAIBLE TO BE USED IN SERVICE.
    test = document.getElementById('tokentext').value
    // PARSE WITH JSON
    JsonParseToken = JSON.parse(test);
    MapServerToken = (JsonParseToken.token);

    alert(MapServerToken );

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.14192 seconds
  • Memory Usage 2,917KB
  • Queries Executed 13 (?)
More Information
Template Usage (34):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (5)bbcode_code
  • (3)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (9)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (9)postbit
  • (9)postbit_onlinestatus
  • (9)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates