0

Hi all i am calling a javascript function on WebView in android. I am sending a JSON data which will pass value to html. My JSON data is in following format i have checked using online tools it is valid.

{"Results":{"Number of Tests":"2","Latency(avg)":"17","Failure":"0%","Latitude":"12° 55' 35.5872''  N","Longitude":"77° 36' 4.16916''  E","Latency(max)":"18","Latency(min)":"17"},"TestStaus":"Passed","Test":"Game Test"}

I am using following code to display parsed result in html using jquery.

var jsonObject = JSON.stringify(vk);
document.write(jsonObject); 
$.each($.parseJSON(jsonObject), function(k, v)
{
     document.write("<tr><td>" + k + "</td><td>" + v + "</td></tr>");
 });

It is giving me output in following manner

  1. Parameter Value
  2. Results [object Object]
  3. TestStatus Passed
  4. Test Game Test

Please help how to read all results. Why it is reading object object.

2
  • What is the problem? "document.write(jsonObject);" output "Object"? This is normal since jsonObject hasn't method toString(). Commented Jan 29, 2014 at 13:24
  • Use console.log(k) and view in the console. You can't document.write a json object. It's too complex to just be added to the page like that. Commented Jan 29, 2014 at 13:25

2 Answers 2

1

Just use recursion. You need to be able to handle multidimensional objects. Also, I usually use jquery for DOM or AJAX only. For something like this, you might not need it.

Your Json

   var vk = {"Results":{
     "Number of Tests":"2",
     "Latency(avg)":"17",
     "Failure":"0%",
     "Latitude":"12° 55' 35.5872''  N",
     "Longitude":"77° 36' 4.16916'' E","Latency(max)":"18",
     "Latency(min)":"17"
      },
     "TestStaus":"Passed",
     "Test":"Game Test"};

Recursive function

     function drawJSON(obj){
      for(var key in obj){
      if(typeof obj[key] === 'object'){
       drawJSON(obj[key]);
       continue;
      }
      document.write("<div><span>" + key + "</span><span>" + obj[key] + "</span> </div>");
      }
    }

    drawJSON(vk);

DEMO

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

Comments

1

The Results is object so it is showing as [object object]. You can do this by:

   function printEach(jsonObject) {
        $.each(jsonObject, function(k, v)
        {
            if(typeof v === 'object') {
                printEach(v);
            } else {
                console.log("<tr><td>" + k + "</td><td>" + v + "</td></tr>");
            }        
        });
    }

    var vk = {"Results":{"Number of Tests":"2","Latency(avg)":"17","Failure":"0%","Latitude":"12° 55' 35.5872''  N","Longitude":"77° 36' 4.16916''  E","Latency(max)":"18","Latency(min)":"17"},"TestStaus":"Passed","Test":"Game Test"};
    var jsonObject = JSON.stringify(vk); 
    printEach($.parseJSON(jsonObject));

You can see the fiddle http://jsfiddle.net/58grs/1/

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.