使用30个blob对象反序列化大型json数据

时间:2017-07-18 08:40:54

标签: c# json deserialization

我有一个webservice,它返回一个json字符串。 Alltogehter我有432个带有blob数据的JSON对象。 在每个Webrequest中,我得到30个物体432,直到我在432。 对于每个反序列化,反序列化的时间增加。 30物体需要9秒,在第二次消毒时我们需要15秒。 463对象是1,3 GB。

如何调整我的代码,30个对象不需要9秒以上?

字符串如下所示:

{
  "d:" [
    {
      "id": "1",
      "groesse": 2515221,
      "byteasbase64string": "the base64 string"
    },
    {
      "id": "2",
      "groesse": 887748,
      "byteasbase64string": "the base64 string"
    }
  ]
}

继承我的c#代码:

List<db.dwaprotokolldatei> getProtokolldatei = new List<db.dwaprotokolldatei>();
using (postStreamGPD = resGPD.GetResponseStream())
{
    using (srGPD = new StreamReader(postStreamGPD))
    {
        string responseFromServerGPD = srGPD.ReadToEnd(); // Long running time
        getProtokolldatei = JsonConvert.DeserializeObject<db.rootobject>(responseFromServerGPD).d;// Long running time
        responseFromServerGPD = "";
    }
}

reqGPD = null;
postDataAsBytesGPD = null;
postStreamGPD.Dispose();
postStreamGPD = null;
resGPD.Dispose();
resGPD = null;
srGPD.Dispose();
srGPD = null;

GC.Collect();

的WebRequest:

reqGPD = (HttpWebRequest)HttpWebRequest.Create("*****");
                        reqGPD.Method = "POST";
                        reqGPD.ContentType = "application/json";
               mobileJSON = ConvertTOJSONMobile(existingMobile);

                        postDataAsBytesGPD = Encoding.UTF8.GetBytes(mobileJSON);
                        postStreamGPD = reqGPD.GetRequestStream();
                        postStreamGPD.Write(postDataAsBytesGPD, 0, postDataAsBytesGPD.Length);
                        postStreamGPD.Flush();
                        postStreamGPD.Dispose();

                            resGPD = reqGPD.GetResponse();

3 个答案:

答案 0 :(得分:0)

在网络配置中,您需要:

<configuration> 
       <system.web.extensions>
           <scripting>
               <webServices>
                   <jsonSerialization maxJsonLength="50000000"/>
               </webServices>
           </scripting>
       </system.web.extensions>
    </configuration> 

答案 1 :(得分:0)

你能尝试并行化吗?

var set1: Set = [2, 3, 5, 7]
var set2: Set = [2, 3, 4, 6]
var set3: Set = [1, 3, 8, 7]
var new: Set = [2,3]

//let or:NSOrderedSet = [set1, set2, set3] // NSOrderedSet
let ArrayOfSets: Array = [set1, set2, set3]
let finalArry: [String] = ArrayOfSets.flatMap {
    if new.isSubset(of: $0) {
        let i = ArrayOfSets.index(of: $0)
        return "Set\(String(describing: i!+1))"
    }
    return ""
    }.filter{ $0 != ""}

print(finalArry) //["Set1", "Set2"]

答案 2 :(得分:0)

ReadToEnd()要快得多。

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                reader.ReadToEnd();
            }