0

My json is:

[
   {
      "_id":{
         "time":1381823399000,
         "new":false,
         "timeSecond":1381823399,
         "machine":263168773,
         "inc":-649466399
      },
      "asset":"RO2550AS1",
      "Salt Rejection":"90%",
      "Salt Passage":"10%",
      "Recovery":"59%",
      "Concentration Factor":"2.43",
      "status":"critical",
      "Flow Alarm":"High Flow"
   },
   [
      {
         "Estimated Cost":"USD 15",
         "AssetName":"RO2500AS1",
         "Description":"Pump Maintenance",
         "Index":"1",
         "Type":"Service",
         "DeadLine":"13 November 2013"
      },
      {
         "Estimated Cost":"USD 35",
         "AssetName":"RO2500AS1",
         "Description":"Heat Sensor",
         "Index":"2",
         "Type":"Replacement",
         "DeadLine":"26 November 2013"
      },
      {
         "Estimated Cost":"USD 35",
         "AssetName":"RO2550AS1",
         "Description":"Heat Sensor",
         "Index":"3",
         "Type":"Replacement",
         "DeadLine":"26 November 2013"
      },
      {
         "Estimated Cost":"USD 15",
         "AssetName":"RO2550AS1",
         "Description":"Pump Maintenance",
         "Index":"4",
         "Type":"Service",
         "DeadLine":"13 November 2013"
      },
      {
         "Estimated Cost":"USD 15",
         "AssetName":"RO3000AS1",
         "Description":"Pump Maintenance",
         "Index":"5",
         "Type":"Service",
         "DeadLine":"13 November 2013"
      },
      {
         "Estimated Cost":"USD 35",
         "AssetName":"RO3000AS1",
         "Description":"Heat Sensor",
         "Index":"6",
         "Type":"Replacement",
         "DeadLine":"26 November 2013"
      }
   ]
]

I need to access it in javascript.

The following code is not working:

var jsonobjstr = JSON.parse(jsonOutput);
alert ("asset: "+jsonobjstr.asset);
3
  • What is jsonOutput set to? Do you have any error messages appearing in the console? Commented Oct 15, 2013 at 18:29
  • This has nothing to do with "java", tag removed. Commented Oct 15, 2013 at 18:30
  • 2
    Hint: you are passing Json inside of an array, treat it as such Commented Oct 15, 2013 at 18:31

2 Answers 2

4

Because the entire JSON is contained in an array.

alert("asset: "+jsonobjstr[0].asset);

http://jsfiddle.net/ExplosionPIlls/yHj5X/2/

Sign up to request clarification or add additional context in comments.

Comments

2

In javascript

var somename = []; means a new array and; var somename = {}; means a new object.

Therefore if some json starts with a [] means it is a array of objects, and if it starts with {} means it is a object.

Your json starts with [], therefore it is a array of objects, so you need to access each object by doing:

json[n].asset for each position of the array (where n is a integer).

BUT:

Your JSON is weird. Looks like you will always have a array with one element (if true, the json should start with {}.

LIKE:

{
    "id":
    {
        "code":1381823399000
    },
    "asset":"RO2550AS1",
    "history":
    [
        {
            "value":"USD 15"
        },
        {
            "value":"USD 15"
        },
        {
            "value":"USD 15"
        }
    ]
 }

Here you can do:

thing.id.code
thing.asset
thing.history[0].value
thing.history[1].value

3 Comments

I disagree with your BUT: He's passing an array with 1 object followed by another array of objects containing uniform information.
There is nothing to disagree. If the parent object is not an array, why treat it like so? Look at the example.
Of course there is something to disagree. Your statement did not say anything at all about "wrapping" the uniform information in a separate property prior to your edit. Without the example to clarify your intention, your statement is simply false, or perhaps incomplete. Regardless, debates are against the rules.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.