-1

I'm new to JSON and Jquery, and I can't find how to extract the values of ProjectCode from this JSON string.

[
    {
        "ProjectID": 3,
        "CLustomerCode": "XYZ001",
        "ProjectCode": "YZPROJ1",
        "Description": "Project1",
        "IssueManager": "iant",
        "NotificationToggle": false,
        "ProjectStatus": null,
        "Added": "/Date(1400701295853}/",
        "Added By": "iant",
        "Changed": "/Date(1400701295853)/",
        "Changed By": "iant"
    },
    {
        "ProjectID": 4,
        "CustomerCode": "XYZ001",
        "ProjectCode": "XYXPROJ2",
        "Description": "Projecton:Project2",
        "IssweManager": "iant",
        "NotificationToggle": false,
        "Projectstatus": null,
        "Added": "lDate(1400701317980)/",
        "AddedBy": "iant",
        "Changed": "/Date(1400701317980)/",
        "Changed By": "iant"
    }
]

The string above is from a variable called data that is the return value from stringify. I expected to be able to do something like

string proj = data[i].ProjectCode;

but intellisense doesn't include any of the properties.

I know very little about JSON - any help appreciated.

Thanks for reading.

Andy
  • 61,948
  • 13
  • 68
  • 95
radiator
  • 51
  • 13
  • 5
    well if you post an image of code, at least make the effort to indent it... Thank you! http://stackoverflow.com/questions/6565281/iterating-over-array-of-objects-javascript-odd-behaviour – A. Wolff May 22 '14 at 16:43
  • 1
    Why why why would you use an image? Why? – Andy May 22 '14 at 16:47
  • Just wondering what will happen to your question once this image wouldn't be anymore online... SO is not your personal helpdesk! – A. Wolff May 22 '14 at 16:47
  • Thanks for nothing wolfy – radiator May 22 '14 at 16:53
  • Might be a good idea to read the [Help Center](http://stackoverflow.com/help) and lurk for awhile to figure out how things work here. Hint: Don't use images to post code. – Robert Harvey May 22 '14 at 16:56
  • Thanks Rob - got that immediately after posting lol - btw you handled guiding a newbie much better which is appreciated. – radiator May 22 '14 at 17:11

4 Answers4

2

Use parseJSON:

var obj = jQuery.parseJSON("{ 'name': 'Radiator' }");
alert(obj.name);
Donal
  • 31,121
  • 10
  • 63
  • 72
0

You need to loop through each object returned in the response and get the ProjectCode property inside each one. Assuming the data variable is your JSON this should work:

$.each(data, function(i, obj) {
    console.log(obj.ProjectCode);
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

Use JSON.parse():

var a; // Your JSON string
var b = JSON.parse(a);  //Your new JSON object
//You can access Project code, use index i in b[i].ProjectCode in a loop
var projectCode = b[0].ProjectCode;  
Trader
  • 249
  • 4
  • 10
0

You should post the raw code so its easier to visualize this stuff. Since what you are looking for is the list of ProjectCodes (in this case - ["XYZPROJ1", "XYZPROJ2"]).

It seems like what we have is an array or list ([...]) of projects. Where each project has a ProjectID, CustomerCode, ProjectCode, Description and so on...

So lets assume data points at this JSON blob. Here is how you would go about accessing the ProjectCode:

// Access the "i"th project code
var p_i_code = data[i].ProjectCode;

// How many projects?
var num_projects = data.length; // since data is a list of projects

// Want the list of project codes back? (I use underscore.js)
var project_codes = _.map(data, function(project) {
    return project.ProjectCode;
});
sabhiram
  • 897
  • 7
  • 10