JSON有任何Checksum机制吗?

时间:2013-04-09 05:38:04

标签: json web-services checksum

我需要将大量JSON数据上传到Web服务。什么是分析服务器正确接收数据并上传所有数据的最佳方法?如果有人有这方面的经验,请告诉我。感谢。

3 个答案:

答案 0 :(得分:2)

您可以检出我的项目: https://github.com/hidayetcolkusu?tab=repositories

计算:

ChecksumCalculator checksumCalculator = new ChecksumCalculator();

string json = @"{""Name"":""Hidayet Raşit"",""Surname"":""ÇÖLKUŞU""}";

ushort checksum = checksumCalculator.Calculate(json);
Result: 43460

比较:

ChecksumCalculator checksumCalculator = new ChecksumCalculator();

string json = @"{""Name"":""Hidayet Raşit"",""Surname"":""ÇÖLKUŞU""}";

bool result = checksumCalculator.Compare(json, 43460);
Resut:true

ChecksumCalculator checksumCalculator = new ChecksumCalculator();

string json = @"{""Name"":""Hidayet Raşit"",""Surname"":""ÇÖLKUŞU"",""Checksum"":43460}"; 

bool result = checksumCalculator.Compare(json);
Result:true

答案 1 :(得分:1)

您可以像这样计算json的哈希值:

var sha1 = System.Security.Cryptography.SHA1.Create();
byte[] buf = System.Text.Encoding.UTF8.GetBytes(jsonString);
byte[] hash= sha1.ComputeHash(buf, 0, buf.Length); 
var hashstr = System.BitConverter.ToString(hash).Replace("-", "");

答案 2 :(得分:0)

您可以计算 md5 来比较两个json。

public static string CreateMD5(string json)
{
    // Use json string to calculate MD5 hash
    using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
    {
        byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(json);
        byte[] hashBytes = md5.ComputeHash(inputBytes);

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < hashBytes.Length; i++)
        {
            sb.Append(hashBytes[i].ToString("X2"));
        }
        return sb.ToString();
    }
}