0

Question revised for clarity: Im having issue parsing json file. I want to output the value of the basename labeled "image_01". So the it will only outputs http://blog.url/news/images/201516.jpg

<div id="placeholder"></div>
<script>
var data={
    "items": [
        {
            "title": "This is The Title",
            "customFields": [
                {
                    "basename": "image_01",
                    "value": "<form mt:asset-id=\"352706\" class=\"mt-enclosure mt-enclosure-image\" style=\"display: inline;\"><a href=\"http://blog.com/news/images/20150116.jpg\">20150116.jpg</a></form>"
                }
            ],
            "permalink": "http://blog.com/index.php"
        }
    ]
}

document.getElementById("placeholder").innerHTML=data.items[0].title + " " + data.items[0].permalink+"--"+ data.items[0].customFields.basename.image_01;
</script>

http://jsfiddle.net/v57s2csa/ fixed

I think particular problem lies in

data.items[0].customFields.basename.image_01;

3
  • 1
    There is a problem with your json. Check it via jsonlint online. Commented Mar 10, 2015 at 9:15
  • You're absolutely right @T.J.Crowder. It was certainly tedious for me (mandatorily) hopping over elsewhere to check out code. Commented Mar 10, 2015 at 9:23
  • That's not JSON, that's JavaScript code. And this has nothing to do with parsing. Commented Mar 10, 2015 at 9:32

3 Answers 3

1

customFields is an array. You need to access the values inside it with an index.

document.getElementById("placeholder").innerHTML=data.items[0].title + " " + data.items[0].permalink+"--"+ data.items[0].customFields[0].basename;
// Change is here -------------------------------------------------------------------------------------------------------------------^^^

http://jsfiddle.net/v57s2csa/1/

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

1 Comment

This one is correct and you (Maca) also mentioning the value in the statement (...basename.image_01), where the statement is completely incorrect. we must stop till the name to access its corresponding name
0

Replace the javascript with:

  document.getElementById("placeholder").innerHTML=data.items[0].title + " " 
+ data.items[0].permalink+"--"+ data.items[0].customFields[0].basename.image_01;

to reflect that customFields is array, not object.

Comments

0

There is no object called image_01 inside data. Instead of

data.items[0].customFields.basename.image_01;

use

data.items[0].customFields[0].value

Also a img tag inside Form to display image after appending,

http://blog.url/news/images/201516.jpg\"><img src='http://blog.url/news/images/201516.jpg'/></a></form>

Comments

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.