Solved

How to count json data

Posted on 2016-09-14
2
56 Views
Last Modified: 2016-09-14
Hi Experts,


I want to count the json data in it and i tried with
var propertyData = JSON.parse(rsp);
var length =  Object.keys(propertyData).length;

Open in new window

. but my json lenght is 1 . Instead function returning me the 6 which is each property of json . how can i get rid of it

My Json
"{\r\n  \"FullImage\": \"http://localhost:57226/img/image1.JPG\",\r\n  \"imageThumb\": \"http://localhost
:57226/img/image1.JPG\",\r\n  \"description\": \"Kitchen\",\r\n  \"Address\": \"Test\",\r\n  \"imageid
\": 1,\r\n  \"TextColor\": \"White\"\r\n}"

Open in new window


Thanks Experts
0
Comment
Question by:karthik80c
2 Comments
 
LVL 35

Accepted Solution

by:
YZlat earned 500 total points
ID: 41798744
Object.keys(propertyData).length is the wrong thing to use as it will return the property count. instead try converting your json string to an array of objects and then getting the count

var jsonObj = $.parseJSON('[' + propertyData + ']');
jsonObj.length;

Open in new window

0
 
LVL 32

Expert Comment

by:ste5an
ID: 41798756
The question is how you handle the JSON and the real content of rsp..

Counting is simple:

<!DOCTYPE html>
<html>
<body>
	<script type="text/javascript" src="jquery-3.1.0.min.js"></script>
	<script type="text/javascript">
		$(document).ready(function() {
			var data = 		
			{  
				"FullImage": "http://localhost:57226/img/image1.JPG",  
				"imageThumb": "http://localhost:57226/img/image1.JPG", 
				"description": "Kitchen",  
				"Address": "Test",
				"imageid": 1, 
				"TextColor": "White"
			};
			var json = JSON.stringify(data);
			var fromJson = JSON.parse(json);
			var length =  Object.keys(fromJson).length;
			$("#result").text(length);
		});
	</script>
	<div id="result">..</div>
</body>
</html>

Open in new window

1

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now