Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have stored the JSON file "ftp://mysite.com/json1.json" in my root server folder.
This should mean that "http://www.mysite.com/json1.json" holds true?

I am trying to use the search box to find the first:"thefirstname" and then printout the rest.

I am not getting anything in the last three text boxes.

HTML
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.1.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script type="text/javascript">
        function process() {
        	$.getJSON("http://www.mysite.com/json1.json"){
        		$each(function(key,val){
        			if ($("#text1").val()="thefirstname" && key="first"){
	        	            $("#firstname").val(key.first);
		                    $("#lastname").val(key.last);
		                    $("#description").val(key.desc);
			         }
			});
	      	});
        }
    </script>
      <form name="myform" action="jQueryJson.htm">
	    <input type="text" name="text1" value="thefirstname" size="30" />                    <tr><td align="center" colspan="3">
<input type="button" name="search" value="Search for FirstName" size="50" onclick="process" /></td><td align="center"></td><td align="center">
</td></tr>
<input type="text" name="firstname" value="" size="4"/></td><td align="center">
<input type="text" name="lastname" value="" size="4"/></td><td align="center">
<input type="text" name="description" value="" size="4"/>
	</form>


json1.json
[
	{"first":"fgdfb", last":"dfghdfgh", "desc":"jjif"},
	{"first":"jdtyhjn", last":"hmbnmgh", "desc":"hfhhfyif"},
	{"first":"fgbnfgjthj", last":"tewgsdfgngh", "desc":"argrayif"},
	{"first":"kuykyky", last":"bfbffc", "desc":"lykghif"},
	{"first":"thefirstname", last":"dsfhngh", "desc":"pgkkgff"},
	{"first":"ghjjhb", last":"fdasfgh", "desc":"nfhdfhjif"},
	{"first":"ohhklhjb", last":"qsdwdgh", "desc":"vfrfeif"}
]


What I have tried:

Tried using a button but this was more straightforward.
Posted
Updated 6-Feb-17 3:35am
v8

1 solution

There are quite a number of syntax errors, for example , equal operator should be "==" not "=". There are also missing quotes in the json file. However, I shall not go into such matters. Your question is really about how to loop thru the array of json objects and access their properties (keys).
Try and adapt from this demo:
HTML
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $.getJSON("json1.json", function(result){
            $.each(result, function(i, obj){
                $("div").append(obj.first + " " + obj.last + " " + obj.desc + " " + "<br>");
            });
        });
    });
});
</script>
</head>
<body>

<button>Get JSON data</button>

<div></div>

</body>
</html>
Assuming the "json1.json" is in the same directory of the html file and contains:
[
	{"first":"fgdfb", "last":"dfghdfgh", "desc":"jjif"},
	{"first":"jdtyhjn", "last":"hmbnmgh", "desc":"hfhhfyif"},
	{"first":"fgbnfgjthj", "last":"tewgsdfgngh", "desc":"argrayif"},
	{"first":"kuykyky", "last":"bfbffc", "desc":"lykghif"},
	{"first":"thefirstname", "last":"dsfhngh", "desc":"pgkkgff"},
	{"first":"ghjjhb", "last":"fdasfgh", "desc":"nfhdfhjif"},
	{"first":"ohhklhjb", "last":"qsdwdgh", "desc":"vfrfeif"}
]
 
Share this answer
 
v4
Comments
teledexterus 6-Feb-17 10:12am    
Neither this nor that printouts the contents of json1.json in the div
$(document).ready(function(){
$.getJSON("json1.json", function(result){
$.each(result, function(i, obj){
$("div").append(obj.first + " " + obj.last + " " + obj.desc + " " + "<br>");
});
});
});
Peter Leow 6-Feb-17 11:05am    
Yes, it works. Do not use chrome if you are not using a web server, use firefox instead to test it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900