Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

i got a ajax json response in my javascript page as below


{"result":"[VSummary [userName=kk, plan=Professional, sDate=null, plate=EVA 5653, groupName=null, vehicleType=null, defaultDriver=null, solrQuery=null, driver=null, firstActiveTime=0, lastActiveTime=0, distance=0.0, maxSpeed=0.0, exceptions=0, trips=0, fuelConsumed=0.0, movHrs=0.0, stopHrs=0.0, idlingHrs=62000.0, engineHrsOn=0.0, engineHrsOff=0.0, workingHrs=0.0]]"}


Here VSummary is a list of object retured from my java application. How can i convert VSummary to list in JAVASCRIPT

JSON.parse is not working here

var a=JSON.parse(response.result); // here response is the json response
SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data

What I have tried:

JavaScript
var response = JSON.parse('{"result":"[VSummary [userName=kk, plan=Professional, sDate=null, plate=EVA 5653, groupName=null, vehicleType=null, defaultDriver=null, solrQuery=null, driver=null, firstActiveTime=0, lastActiveTime=0, distance=0.0, maxSpeed=0.0, exceptions=0, trips=0, fuelConsumed=0.0, movHrs=0.0, stopHrs=0.0, idlingHrs=62000.0, engineHrsOn=0.0, engineHrsOff=0.0, workingHrs=0.0]]"}');

JavaScript
var a=JSON.parse(response.result);  

SyntaxError: JSON.parsed character at line 1 column 2 of the JSON data


JavaScript
var a=response.result["VSummary"]

undefined
Posted
Updated 8-Mar-17 2:27am
v2
Comments
Karthik_Mahalingam 8-Mar-17 4:56am    
its a valid json, but not a valid json to get execute this
var a=response.result["VSummary"]
Prajith MP 8-Mar-17 5:11am    
then how can i extract the value
"[VSummary [userName=kk, plan=Professional, sDate=null, plate=EVA 5653, groupName=null, vehicleType=null, defaultDriver=null, solrQuery=null, driver=null, firstActiveTime=0, lastActiveTime=0, distance=0.0, maxSpeed=0.0, exceptions=0, trips=0, fuelConsumed=0.0, movHrs=0.0, stopHrs=0.0, idlingHrs=62000.0, engineHrsOn=0.0, engineHrsOff=0.0, workingHrs=0.0]]"
Karthik_Mahalingam 8-Mar-17 6:24am    
Always use  Reply  button, to post Comments/query to the user, so that the user gets notified and responds to your text.

response.result is this

[VSummary [userName=kk, plan=Professional, sDate=null, plate=EVA 5653, groupName=null, vehicleType=null, defaultDriver=null, solrQuery=null, driver=null, firstActiveTime=0, lastActiveTime=0, distance=0.0, maxSpeed=0.0, exceptions=0, trips=0, fuelConsumed=0.0, movHrs=0.0, stopHrs=0.0, idlingHrs=62000.0, engineHrsOn=0.0, engineHrsOff=0.0, workingHrs=0.0]]


so that is what you are trying to parse when you execute

var a=JSON.parse(response.result); 


however that text is not JSON so you can't use JSON.parse to parse it, you'd need to write your own js code to parse it or update the java code that is sending the response to return you valid json.
 
Share this answer
 
This may not be a proper method to parse a JSON data, but the response you are getting is not a valid JSON format, so you shall try this if in case the response will be in the same format on all cases.

var response = JSON.parse('{"result":"[VSummary [userName=kk, plan=Professional, sDate=null, plate=EVA 5653, groupName=null, vehicleType=null, defaultDriver=null, solrQuery=null, driver=null, firstActiveTime=0, lastActiveTime=0, distance=0.0, maxSpeed=0.0, exceptions=0, trips=0, fuelConsumed=0.0, movHrs=0.0, stopHrs=0.0, idlingHrs=62000.0, engineHrsOn=0.0, engineHrsOff=0.0, workingHrs=0.0]]"}');
     var a = response.result;
    a=  a.replace('[', '(').replace(']]', ']');
    a = a.replace('[', ':{').replace(']', '}');
    a = a.replace(/, /g, '","').replace(/=/g, '":"');
    a = a.replace('{', '{"').replace('}', '"}').replace('(', "{") + '}';
    a = a.replace("VSummary", '"VSummary"');
    var obj = JSON.parse(a);
    var userName = obj.VSummary.userName; // KK
    var plan = obj.VSummary.plan;  // Professional
    var plate = obj.VSummary.plate; // EVA 5653
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900