如何解析我的json结果 [英] How to parse my json result

查看:77
本文介绍了如何解析我的json结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





i在我的javascript页面中得到了一个ajax json响应,如下所示$ / b $ b



{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]]}





这里 VSummary 是从我的java应用程序中退出的对象列表。如何将VSummary转换为JAVASCRIPT列表



JSON.parse在这里不起作用



var a = JSON.parse(response.result); //这里响应是json响应

语法错误:JSON.parse:JSON数据第1行第2列的意外字符



我尝试过:



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:

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=JSON.parse(response.result);  



SyntaxError:JSON数据第1行第2列的JSON.parsed字符






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


var a=response.result["VSummary"]



undefined

推荐答案

response.result是这个



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); 





但是文本不是JSON所以你不能使用JSON.parse来解析它,你需要自己编写用于解析它的js代码或更新发送响应的java代码以返回有效的json。



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.


这可能不是解析JSON数据的正确方法,但是你的响应是获取不是有效的JSON格式,因此如果在所有情况下响应的格式相同,则应尝试此操作。



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


这篇关于如何解析我的json结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆