小编Pau*_*zen的帖子

从JToken获取可能不存在的价值(最佳实践)

使用Json.NET检索C#中甚至可能不存在的JSON值的最佳实践是什么?

现在我正在处理一个JSON提供程序,它返回有时包含某些键/值对的JSON,有时则不然.我一直在使用(也许是错误的)这个方法来获取我的值(例如获得一个double):

if(null != jToken["width"])
    width = double.Parse(jToken["width"].ToString());
else
    width = 100;
Run Code Online (Sandbox Code Playgroud)

现在它工作正常,但是当它们很多时它很麻烦.我最后编写了一个扩展方法,并且只有写完之后我才会想知道是否我是愚蠢的......反正这里是扩展方法(我只包括双和字符串的情况,但实际上我有很多更多):

public static T GetValue<T>(this JToken jToken, string key,
                            T defaultValue = default(T))
{
    T returnValue = defaultValue;

    if (jToken[key] != null)
    {
        object data = null;
        string sData = jToken[key].ToString();

        Type type = typeof(T);

        if (type is double)
            data = double.Parse(sData);
        else if (type is string)
            data = sData;

        if (null == data && type.IsValueType)
            throw new ArgumentException("Cannot parse type \"" …
Run Code Online (Sandbox Code Playgroud)

c# json.net

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

WP7 App在部署后立即崩溃

好的,所以我正在为Windows Phone开发一个应用程序,我似乎无法弄清楚这个问题是什么.

首先,我应该说我的应用程序编译没有警告,没有错误,nada.看起来像一个魅力.

但是,当我将应用程序构建到模拟器或我手头上的两个不同WP7设备中的任何一个时,启动屏幕会显示一瞬间,然后我将返回到设备的主屏幕.

看输出:

'taskhost.exe' (Managed): Loaded 'mscorlib.dll'
'taskhost.exe' (Managed): Loaded 'System.Windows.RuntimeHost.dll'
'taskhost.exe' (Managed): Loaded 'System.dll'
'taskhost.exe' (Managed): Loaded 'System.Windows.dll'
'taskhost.exe' (Managed): Loaded 'System.Core.dll'
'taskhost.exe' (Managed): Loaded 'System.Xml.dll'
'taskhost.exe' (Managed): Loaded '\Applications\Install\6D7C6AA5-7D7C-4056-8BF7-1097F7FBAC40\Install\Subsplash.ExampleLibrary.dll', Symbols loaded.
'taskhost.exe' (Managed): Loaded '\Applications\Install\6D7C6AA5-7D7C-4056-8BF7-1097F7FBAC40\Install\ClientCore.dll', Symbols loaded.
'taskhost.exe' (Managed): Loaded 'System.Xml.Linq.dll'
'taskhost.exe' (Managed): Loaded 'Microsoft.Phone.dll'
'taskhost.exe' (Managed): Loaded 'Microsoft.Phone.Interop.dll'
The thread '<No Name>' (0xda1003e) has exited with code 0 (0x0).
The thread '<No Name>' (0xd660032) has exited with code 0 (0x0).
The program '[206110770] taskhost.exe: Managed' …
Run Code Online (Sandbox Code Playgroud)

.net crash exception windows-phone-7

22
推荐指数
1
解决办法
3883
查看次数

PHP Regex检查两个字符串是否共享两个公共字符

我刚刚开始了解正则表达式,但经过相当多的阅读(并且学习了很多)后,我仍然无法找到解决这个问题的好方法.

让我说清楚,我明白这个特殊问题可能更好地解决使用正则表达式,但为了简洁起见,我只想说我需要使用正则表达式(相信我,我知道有更好的方法来解决这个问题) ).

这是问题所在.我给了一个大文件,每行的长度恰好是4个字符.

这是一个定义"有效"行的正则表达式:

"/^[AB][CD][EF][GH]$/m" 
Run Code Online (Sandbox Code Playgroud)

在英语中,每一行在位置0处具有A或B,在位置1处具有C或D,在位置2处具有E或F,并且在位置3处具有G或H.我可以假设每行将精确地为4个字符长.

我正在尝试做的是给出其中一行,匹配包含2个或更多共同字符的所有其他行.

以下示例假定以下内容:

  1. $line 始终是有效的格式
  2. BigFileOfLines.txt 仅包含有效行

例:

// Matches all other lines in string that share 2 or more characters in common
// with "$line"
function findMatchingLines($line, $subject) {
    $regex = "magic regex I'm looking for here";
    $matchingLines = array();
    preg_match_all($regex, $subject, $matchingLines);
    return $matchingLines;
}

// Example Usage
$fileContents = file_get_contents("BigFileOfLines.txt");
$matchingLines = findMatchingLines("ACFG", $fileContents);

/*
 * Desired return value (Note: this is an example set, there 
 * could be …
Run Code Online (Sandbox Code Playgroud)

php regex preg-match-all

12
推荐指数
1
解决办法
1327
查看次数

标签 统计

.net ×1

c# ×1

crash ×1

exception ×1

json.net ×1

php ×1

preg-match-all ×1

regex ×1

windows-phone-7 ×1