Solved

How to count json data

Posted on 2016-09-14
2
101 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 34

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

739 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