Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following url:
http://localhost/arDemo/assets/www/UI/ProjectList/ProjectList.html?id=1
I am getting the value 1 from the url with this code:
var str = jQuery(location).attr('href');
var url = str.split("=")[1];

I have the following Json data:</pre>

{"status":"1","error":"0","data":[{"project_id":"1","project_name":"Hoysala ACE Phase II","project_ar_url":"www.myfourwalls.in\/projects_data\/villa\/2BHK.wt3","project_ar_type":"Apartment","project_ar_title":"Exterior"},{"project_id":"2","project_name":"WILASA Aikya","project_ar_url":"www.myfourwalls.in\/projects_data\/villa\/table.wt3","project_ar_type":"Plots","project_ar_title":"Exterior"},{"project_id":"3","project_name":"Bluejay Ardley","project_ar_url":"www.myfourwalls.in\/projects_data\/villa\/villa.wt3","project_ar_type":"Villa","project_ar_title":"Interior"},{"project_id":"4","project_name":"Hoysala Habitat","project_ar_url":"www.myfourwalls.in\/projects_data\/villa\/seater_sofa.wt3","project_ar_type":"Apartment","project_ar_title":"360 Model"},{"project_id":"5","project_name":"AVS","project_ar_url":"www.myfourwalls.in\/projects_data\/villa\/bedroom.wt3","project_ar_type":"Villa","project_ar_title":"Exterior"}]}


I want my variable to compare with project_id. If it match, it should get all the details under project_id.
something like if(project_id=$url)


XML
<script type="text/javascript">
        jQuery(document).ready(function () {
            var str = jQuery(location).attr('href');
            var url = str.split("=")[1];
            alert(url);

            var url = "http://myfourwalls.in/services/projects.php?method=get_ar_project&data=";
            jQuery.ajax({
                url: url,
                dataType: 'json',
                success: function (data, textStatus, jqxhr) {
                    CreateProperties(data);
                },
                error: function (jqxhr, textStatus, errorMessage) {
                    console.log(argument);
                }
            });


            jQuery('#content').click(function () {

                Android.NavigateToProjectOverview();
            });

            function CreateProperties(jsonData) {
                for (var i = 0; i < jsonData.data.length; i++) {

                    var designString = '<div> <a id="' + jsonData.data[i].project_id + '">' +
                            '<h3>' + jsonData.data[i].project_name + '</h3>' +
                            '<p>' + jsonData.data[i].project_ar_url + '</p>' +
                            '<p>' + jsonData.data[i].project_ar_type + '</p>' +
                            '<p>' + jsonData.data[i].project_ar_title + '</p>' +
                            '</a></div>';

                    jQuery('#content').append(designString)
                }
                alert(jsonData.data[i].project_id === url);
            }
        });
    </script>
Posted
Updated 19-Feb-15 23:05pm
v2

C#
$(document).ready(function () {
           var obj = ....your string....;

            $(obj.data).each(function () {
                var CurrentProjId = $(this)[0].project_id;
                if (CurrentProjId = WhateverYourConditionIs) {
                    //$(this)[0] is the data you want
                }
            });
});        

Regards..
 
Share this answer
 
Comments
Janardhanam Julapalli 20-Feb-15 5:13am    
please have a look at the question.I have updated it just now!
Thanks7872 20-Feb-15 5:56am    
Why? This solution gives what you want. I have give you whole code. You just need to use it in your code. What is the issue? I have checked it and its working totally fine for me.
Janardhanam Julapalli 20-Feb-15 6:12am    
function CreateProperties(jsonData) {
var items = jsonData.data;

jQuery(items.data).each(function () {
var CurrentProjId = jQuery(this)[0].project_id;
if (CurrentProjId = url) {
//$(this)[0] is the data you want
var project_name=items[i].project_name;
console.log(project_name);
}
});
Janardhanam Julapalli 20-Feb-15 6:12am    
am I doing it right?
Thanks7872 20-Feb-15 6:24am    
What is 'i' in items[i]? There may be an error. Use debugger.
The problem which I can see is in the below code.
JavaScript
var str = jQuery(location).attr('href');
var url = str.split("=")[1];
alert(url);

var url = "http://myfourwalls.in/services/projects.php?method=get_ar_project&data=";

If you closely see the underlined parts, you will notice that both the variables are same.

So, what happens is, the url variable will take the last value. That means, you don't have that project id value anymore.

Rename the variable to resolve this.
JavaScript
var str = jQuery(location).attr('href');
var url_ProjectId = str.split("=")[1];
alert(url_ProjectId);

And compare this value with the id you got from the json.
 
Share this answer
 
Comments
Janardhanam Julapalli 21-Feb-15 0:10am    
How can I compare this value with the value inside the json data. That is my main problem.That is reason y I posted this question.
Janardhanam Julapalli 21-Feb-15 1:45am    
This is working for me
Cool. Please accept the answer and up vote. :)
C#
using jQuery:

var items = jsonData.data;
 for(var i=0; i<items.length; i++){
 var id=items[i].project_id;
if(id=url)
{

var project_name=items[i].project_name;


}

}
 
Share this answer
 
v4
Comments
Janardhanam Julapalli 20-Feb-15 5:25am    
@mgl chandra sekhar -- if(id==url)
{

var project_name=items[i].project_name;

console.log(project_name);

break;
}
this part is being skipped. I noticed in console.
mgl chandra sekhar 25-Feb-15 6:04am    
instead if(id==url) use if(id=url)
XML
<script type="text/javascript">
        jQuery(document).ready(function () {
            var str = jQuery(location).attr('href');
            var url_ProjectId = str.split("=")[1];

            var url = "http://myfourwalls.in/services/projects.php?method=get_ar_project&data=";
            jQuery.ajax({
                url: url,
                dataType: 'json',
                success: function (data, textStatus, jqxhr) {
                    CreateProperties(data);
                },
                error: function (jqxhr, textStatus, errorMessage) {
                    console.log(argument);
                }
            });


            jQuery('#content').click(function () {

                Android.NavigateToProjectOverview();
            });

            function CreateProperties(jsonData) {
               for (var i = 0; i < jsonData.data.length; i++) {
               var id=jsonData.data[i].project_id;

                   if(id==url_ProjectId)
                   {
                       var designString = '<div class="project_info"><a id="' + jsonData.data[i].project_id + '">' +
                            '<h3>' + jsonData.data[i].project_name + '</h3>' +
                            '<p>' + jsonData.data[i].project_ar_type + '</p>' +
                            '<a href="'+jsonData.data[i].project_ar_url+'">' + jsonData.data[i].project_ar_title + '</p>' +
                            '</a></div>';
                            jQuery('#content').append(designString);
                   }
                   else
                   {
                        console.log('error');
                    }

                }
            }
        });
    </script>
 
Share this answer
 

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