0

I'm accessing a back-end server through a wordpress. I put the question here because it is more java related than wordpress related. After I initialize, I get a big chunk of JSON formatted data in my console. I WANT IT.

{"HeroP":0,"Coins":1,"LeadP":0,"Exp":10},"currency1":0,"currency2":0,"currency3":0,"currency4":0,"currency5":0,"currency6":0,"displayName":"testuser":{}}

This happens because of:

function accountDetailsResponse(response) {
console.log(JSON.stringify(response));

That javascript is found in my header.php, not the various js files I have saved.

I love seeing the server working. But I want to isolate "Coins" or "displayName" as a variable. Then I want to define an element in my html like this:

  • Coins
  • displayName
  • Exp - or whatever. Any/all of those things in the JSON

This way, when someone logs in, it automatically shows their display name, Coins, or any other variable in a nice tidy list without the unnecessary JSON value that isn't relevant to them.

Further details: The console spits out nearly everything for debugging purposes. Server test data, API initialization data, the works. Likewise, more than one person will log into this server, so displayName "testuser" will change to displayName "billybob" or "jimmyjoe", whoever logs in.

I'm super new at coding with javascript, and am barely literate. I apologize if the question isn't formatted correctly. I've been figuring things out on my own, but this one definitely has me stuck. I can't seem to parse the string, isolate a variable, and display it in html!

2
  • 2
    JavaScript is not Java. Commented Oct 19, 2017 at 2:43
  • formatting and typos Commented Oct 19, 2017 at 9:29

5 Answers 5

1

You need get JSON objects as a variable as described in this MyKong post. In your case, you likely want some line like:

var coins = response.coins;
Sign up to request clarification or add additional context in comments.

Comments

1

If you're dealing with JSON string:

var obj = JSON.parse(json_string);

Once you have the object, use the dot operator to access properties:

var prop = obj.prop;

OR the array index syntax:

var prop = obj["prop"];

Comments

0

The meaning of that javascript line:

console.log(JSON.stringify(response));

response is some javascript object, containing properties.
JSON.stringify() converts that object into a JSON string.
console.log() writes that string string to the console window of the browser.

So you don't want to read and parse the formatted JSON string that you see in the console, because you already have that information accessible in that response object.

The structure isn't quite clear (the JSON is missing some parts), but try

console.log(response.displayName);

instead of, or next to, your original console.log line. You should see "testuser" in the console. Now you need to find an HTML element to write that value to, instead of the console.

4 Comments

Thanks for the edit, Hans Kesting. I'll being attempting these excellent suggestions! I'll update by the end of the day EST.
Not quite the end of the day... But I did try all of your suggestions. Interestingly, i got a return of "undefined." What I did to get this was first stringify, then create a variable of the parsed
Alright Hans Kesting...I did what you recommended and totally replaced my crap with your simple easy console.log(response.displayName). It worked, and it displayed testuser in the log. When I threw your code into "document.getElementById("Coins").innerHTML = (response.displayName);" It displayed "testuser" as the result for Coins (as you might expect.). HOWEVER, when I change displayName to Coins...the result is undefined. I suspect this is because the value of 1 is not in quotations in the string, and displayName is. How would I fix that?
"testuser" is in quotes because it is a string value, and 1 isn't, because it is an integer value. That is not the problem here. Your (invalid) JSON fragment shows that "Coins" is in an extra set of { }, so that Coins property is at a deeper level. So you need response.something.Coins - but I cannot see from your JSON what that "something" should be. Maybe update your question with the correct (full) JSON?
0

Three for three. Every question I've posted, I've solved! So the syntax was wrong of the parse js.' var obj = (JSON.parse(JSON.stringify(response)));' That allowed me to use obj.coin. It currently returns a value of "undefined." But I have a feeling that is going to be a formatting issue of the string. So that will be a whole different question.

3 Comments

Why would you convert a javascript object (response) to JSON only to convert it back to an object? obj should contain the same data as response
I have no idea. I know JSON means "JavaScript Object....something." I suppose I was under the assumption that the javascript object (response) was not in some sort of special format. I also assumed that JSON was a formatting method that would allow me to more easily parse. FYI, I'm still getting "undefined" as a result. Any idea why?
JSON = JavaScript Object Notation, it's a string version of the real object. You can use it in source code or in communicating with "other" systems. Try a console.dir(response), you then see the structure of the object. Javascript is case sensitive, so watch out for that.
0
`    function accountDetailsResponse(response) {
        console.log (JSON.stringify(response));//logs the string to console
        console.log(response.displayName);
        document.getElementById("Coins").innerHTML = 
        (response.currencies.Coins);`

Hans, thanks again. Your code got what I needed, was precise, and you astutely cut through my awkward beginner question phrasing.

So this code was weird. The account details are is an extensive string with latitude, longitude, and every conceivable character trait on my server. The targeted variable "Coins" actually existed in two catagories. "currencies" and "reservedcurrencies." Both had different values. For some reason, the code was isolating the one that had an undefined value. So I had to get specific with (response.currencies.Coins) to specify which one I wanted. It immediately popped up with the value I was looking for on my html element. I see now that NO ONE could have helped me because I didn't include the entirety of my string. So the "undefined" result would have been impossible to diagnose.

Big lesson I learned here...don't go to the pros without details. And strings can have..."sub"strings. Or something.

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.