2017-06-27 37 views
0

这是我的包装类,显示值在顶点控制器

public class JSON2Apex { 

    public class JSON2Apex { 
     public List<Results> results; 
     public String status; 
    } 

    public class Location { 
     public Double lat; 
     public Double lng; 
    } 


    public static JSON2Apex parse(String json) { 
     return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class); 
    } 
} 

我的顶点控制器,

public class callout 
{ 
    @future(callout=true) 
    public static void callout(String add,Id cntid) 
    { 
     Http http = new Http(); 
     HttpRequest req = new HttpRequest(); 
     req.setEndpoint('https://maps.googleapis.com/maps/api/geocode/json?address='+add+'&key=AIzaSyDvf7W2SWXwUPXy8X2PSIGM6NwVusZywx4'); 
     req.setMethod('GET'); 
     HTTPResponse res = http.send(req); 
     if(res.getStatusCode()==200) 
     { 
      JSON2Apex2 obj = (JSON2Apex2)JSON.deserialize(res.getBody(), JSON2Apex2.class); 
      System.debug('Here I want to diplay lat and lng values'); 
     } 
    } 
} 

在这里,我想显示在顶部的定位方法纬度和经度值控制器,如何做到这一点?

回答

0

目前你不能因为你没有内部类的实例作为外部类的属性,所以它不会作为类的一部分公开。你需要一个像currentLocation或类似的属性。

编辑答案更新属性名称:

public class JSON2Apex { 

    public Location location {get; set;} 

    public class JSON2Apex { 
     public List<Results> results; 
     public String status; 
    } 

    public class Location { 
     public Double lat; 
     public Double lng; 
    } 


    public static JSON2Apex parse(String json) { 
     return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class); 
    } 
} 

然后你的其他代码示例中这将是

System.debug('Here I want to diplay lat and lng values' + obj.location.lat + ' ' + obj.location.lng); 
+0

没有,这表明试图引用空对象error.There是没有价值在当前位置 –

+0

你的json响应是什么样的? –

+0

我不知道你的json是什么样子,为什么我说像当前位置或类似的东西。但只是进行类似的调用,房产名称最有可能是“位置”。所以我会更新答案。 –