Pregunta

Here I am getting the data using the java.stringify. But to use the data which I get in the graph framework, that should not be withinquoted. I need to parse all the data which has quote in it. (eg [{data:[{x:87.6,y:85}..) What should I do here? Please help me!!

Here`s the data which I need to parse..

[{"data":[{"x":87.6,"y":85},{"x":116.08,"y":61},{"x":113.11,"y":49},{"x":181.37,"y":65},{"x":138.14,"y":74},{"x":66.03,"y":89}]}]
¿Fue útil?

Solución

Use regular expression to remove quotes.

For example if it is a json string you can do this:

var json = '{ "name": "John Smith" }';    //Let's say you got this
json = json.replace(/\"([^(\")"]+)\":/g,"$1:");    //This will remove all the quotes 
json;     //'{ name:"John Smith" }'

In case of your input:

var a ='[{"data":[{"x":87.6,"y":85},{"x":116.08,"y":61},{"x":113.11,"y":49},{"x":181.37,"y":65},{"x":138.14,"y":74},{"x":66.03,"y":89}]}]';
a = a.replace(/\"([^(\")"]+)\":/g,"$1:");    
a;    //"[{data:[{x:87.6,y:85},{x:116.08,y:61},{x:113.11,y:49},{x:181.37,y:65},{x:138.14,y:74},{x:66.03,y:89}]}]"

Otros consejos

A JSON is like an array of elements.Each elements can be again an array of elements with index being a charecter instead of numbers. so in ur example,there are multitudes of arrays. 1.Taking away the first wrapper array ,you reach to 'data' arrray 2.

    var js=[{"data":[{"x":87.6,"y":85},{"x":116.08,"y":61},{"x":113.11,"y":49},{"x":181.37,"y":65},{"x":138.14,"y":74},{"x":66.03,"y":89}]}];

    //This will have all the data value in array.
    var data=js[0]['data'];
//The data array has lots of sub arrays whose elements are arrays (with index x,y).
    for(var i=0;i<data.length;i++){
    var subData=data[i];
    var x=subData['x'];
    var y=subData['y'];
    //Do something with x and y
    }

In this way you can reach out to any elements within JSON for parsing .Hope this helps !!! JS Fiddle Link to Check:http://jsfiddle.net/saiprasad2k6/f88fH/7/

In Chrome console i tried this

var obj = jQuery.parseJSON('{"data":[{"x":87.6,"y":85},{"x":116.08,"y":61},{"x":113.11,"y":49},{"x":181.37,"y":65},{"x":138.14,"y":74},{"x":66.03,"y":89}]}');

obj

I am getting output as

Object {data: Array[6]}

I hope from this you can get an idea.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top