Solved

How to count json data

Posted on 2016-09-14
2
81 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!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IEnumerable<T> to a List<T> 8 40
Amazon S3 .Net error 5 20
C# Formatting DateTime String Value 5 34
c# DateTime Format validation 3 40
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!
This article discusses how to create an extensible mechanism for linked drop downs.
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

832 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