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
    521
    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 11: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
    521
    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
    521
    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



Recent Articles