小编Fan*_* Wu的帖子

如何在objective-c中使用NSNull创建if语句

我正在开发一个iPhone应用程序,我需要使用JSON它从服务器接收数据.在iPhone方面,我将数据转换为NSMutableDictionary.

但是,日期类型数据为空.

我用下面的句子来读日期.

NSString *arriveTime = [taskDic objectForKey:@"arriveTime"];
NSLog(@"%@", arriveTime);

if (arriveTime) {
    job.arriveDone = [NSDate dateWithTimeIntervalSince1970:[arriveTime intValue]/1000];
}
Run Code Online (Sandbox Code Playgroud)

当arrivalTime为null时,如何创建if语句.我试过[到达时间长度]!= 0,但我不工作,因为arriTime是一个NSNull并且没有这个方法.

iphone json nsmutabledictionary nsnull

16
推荐指数
1
解决办法
7517
查看次数

从struts2动作返回JSON消息

我在我的Web应用程序中使用jquery和struts2.现在我需要将谷歌地图嵌入我的网页并添加一些标记.我使用jquery.getJSON()命令向struts2动作发送请求.

在struts.xml中

<package name="ajax" namespace="/ajax" extends="json-default">
    <action name="LatLngList" class="com.test.ajax.Action" method="find">
        <result type="json"></result>
    </action>
</package>
Run Code Online (Sandbox Code Playgroud)

在网页中

<script type="text/javascript">
    function readLatLng() { 
        var latitude = new Array();
        var longitude = new Array();
        $.getJSON("ajax/LatLngList.action?id=9", function(data){
            $.each(data, function(i,latlng){
            latitude.push(latlng.latitude);
            longitude.push(latlng.longitude);       
            });
        });
    }
</script>
Run Code Online (Sandbox Code Playgroud)

在操作中,我从数据库中读取数据并创建对象列表,然后使用jackson json处理器的ObjectMapper将List转换为JSON字符串.最后,我将JSON String作为返回值返回.

public String find() throws JsonGenerationException, JsonMappingException, IOException {

    List latlngList = new ArrayList();
    /*
    latlngList.add(...);
    */

    ObjectMapper mapper = new ObjectMapper();
    String str = mapper.writeValueAsString(latlngList);
    System.out.println(str);
    return str;
}
Run Code Online (Sandbox Code Playgroud)

当我运行Web应用程序时,控制台中显示JSON String:

[
{"latitude":37.7935697,"longitude":121.181969},

{"latitude":37.7852119,"longitude":121.1759833},

{"latitude":37.7858117,"longitude":121.1250904},

{"latitude":37.794129,"longitude":121.1229535},

{"latitude":37.7974078,"longitude":121.0874301}, …
Run Code Online (Sandbox Code Playgroud)

ajax jquery json struts2 jackson

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

json ×2

ajax ×1

iphone ×1

jackson ×1

jquery ×1

nsmutabledictionary ×1

nsnull ×1

struts2 ×1