Solved

How to count json data

Posted on 2016-09-14
2
76 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 33

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

770 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