0

I have json result I have code but it works on only unique keys in my data there are different key for each value
Please help me

{
    "images": [
    {
        "time": 2.86091,
        "transaction":
        {
            "status": "Complete",
            "subject": "test2",
            "confidence": 0.77,
            "gallery_name": "gallerytest1",
        },
        "candidates": [
        {
          "subtest1": "0.802138030529022",
          "enrollment_timestamp": "1416850761"
        },
        {
          "elizabeth": "0.802138030529022",
          "enrollment_timestamp": "1417207485"
        },
        {
          "elizabeth": "0.777253568172455",
          "enrollment_timestamp": "1416518415"
        },
        {
          "elizabeth": "0.777253568172455",
          "enrollment_timestamp": "1416431816"
        }
        ]
    } ]
}

I want to parse all catidates and its values from it elizabeth= 0.77777 subtest1=0.802138030529022

Adam Rice
  • 880
  • 8
  • 20
  • Its a Simple way to work with `json` in `javascript` because there is no need of any third party library to access the `data `. from any complex `json`. Just simply Iterate through out the array . Thanks @user1199396 – Vikrant Kashyap Apr 03 '16 at 09:57
  • This is not JSON, and you are not "parsing it". It is a **JavaScript object**, and you are **accessing** it. Anyway, when you say *I want to parse all catidates and its values from it elizabeth= 0.77777 subtest1=0.802138030529022*, I cannot understand. Where is 0.777777 coming from? How do I know which elizabeth value you want to retrieve? What does `subtest1` mean? –  Apr 03 '16 at 11:54

3 Answers3

0

JSON is already 'parsed' for Javascript as JSON means JavaScript Object Notation. If your question is that you don't know how to loop through the candidates of your JSON object, here is how you do it:

var candidates = jsonObject.images[0].candidates
for (var key in candidates) {
        alert(jsonObject[key]);
}
Laurens
  • 2,596
  • 11
  • 21
  • My Key of each candidates object is different so How it iterate using for loop – user1199396 Apr 03 '16 at 10:21
  • that's why you have to use a loop instead of specifing the keys yourself. Just use the for each loop as specified in my answer. You don't need the key names (as the variable `key` holds this) – Laurens Apr 03 '16 at 10:23
0

You have error in your json string, check this line: "gallery_name": "gallerytest1", the char , is the problem.

code example for parsing:

var data = JSON.parse(jsonString);
alert(data["images"][0]["candidates"][0]["subtest1"]);

UPDATE:

Here is an example how to work with dinamic properties.

    var jsonData = JSON.parse(jsonString);

        for(i=0; i < jsonData["images"].length; i++) { // foreach image
            var candidates = jsonData["images"][i]["candidates"]; // array of candidates

            for (j = 0; j < candidates.length; j++) { // foreach candidate
                var candidate = candidates[j]; // single candidate

                for (var key in candidate){ // foreach candidate property

                    // this if condition is required when working with for loop on this way.
                    // for more visit http://stackoverflow.com/questions/9329446/for-each-over-an-array-in-javascript
                    if (typeof candidate[key] === 'function') {
                       continue;
                    }
                    alert("Key: " + key + "\nValue: " + candidate[key]);
                }
            }
        }
Boy
  • 1,182
  • 2
  • 11
  • 28
0

just simply referring your jsonObject to a Variable var jsonData and then use simple loop to Iterate through the complex jsonObject as per your json Structure,

var jsonData = {.....}  // Your JSON Object that you added in your Question.

here jsonData contains your complex jsonObject Now Iterating through the json elements.

for(i=0; i < jsonData.images.length; i++){ //iterate throughout the Images Node Array.

    var candidates = obj.images[i].candidates; //Get the Candidate Node of each Image.

    for(j=0; j < candidates.length; j++){ //iterate throughout the Candidates Node Array.

       console.log(candidates[j]); // get the each Candidate to print on Console log of Web Browser.
  }
Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52