C读取对有意义对象的OData$批处理多部分/混合响应

C读取对有意义对象的OData$批处理多部分/混合响应,odata,Odata,我正在使用OData服务,我正在使用RestSharp成功地将我的请求发布到/$batch endpoint并获得响应。响应头包含 "Content-Type" : "multipart/mixed; boundary=<GUID>" 如何在我的C代码中反序列化和提取JSON对象?我不想发明一个正则表达式模式,这是我最后的选择 我也尝试过使用Simple.OData.Client,但我的请求与Simple.OData.Client不完全兼容

我正在使用OData服务,我正在使用RestSharp成功地将我的请求发布到/$batch endpoint并获得响应。响应头包含

"Content-Type" : "multipart/mixed; boundary=<GUID>"
如何在我的C代码中反序列化和提取JSON对象?我不想发明一个正则表达式模式,这是我最后的选择 我也尝试过使用Simple.OData.Client,但我的请求与Simple.OData.Client不完全兼容

还尝试使用下面的代码提取,但没有必要给我我想要的

var sc = new StringContent(response.Content);
var content = sc.ReadAsStreamAsync().Result;
var streamContent = new StreamContent(content);
streamContent.Headers.ContentType = MediaTypeHeaderValue.Parse(response.ContentType);
var provider = streamContent.ReadAsMultipartAsync().Result;
有人能告诉我提取Json对象的最佳方法吗

谢谢
Nero

我设法让这个工作的HttpClient和

下面是代码

using (var httpClient = new HttpClient())
{
    using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://service-url/Entity/v1.3/$batch"))
    {
        request.Headers.TryAddWithoutValidation("Accept", "application/json");
        request.Headers.TryAddWithoutValidation("Client-Id", "XXXXXXXXX");  
        // Add all the headers here
 
        // this is your custom batch request
        request.Content = new StringContent("--batch\ncontent-type: multipart/mixed;boundary=changeset\n\n--changeset\ncontent-type: application/http\nContent-Transfer-Encoding: binary\n\nPOST CarEntries HTTP/1.1\ncontent-type: application/json;charset=utf-8\naccept: application/json;\n\n{\n\"RefId\": \"Test\",\n\"Child\": {\n\"ChildId\": \"412000415\"\n}\n}\n--changeset--\n--batch--");
        request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/mixed;boundary=batch"); // This is imporatnt - but please refer to your api documentation 

        var response = await httpClient.SendAsync(request);
        response.EnsureSuccessStatusCode();
        var multiPartContent = await response.Content.ReadAsMultipartAsync(); // This is part of the extension
        var mixedContent = multiPartContent.Contents.First(); // you will have multiple contents, select the content you want
        
        var data = await mixedContent.ReadAsStringAsync();// read it as string
        
        Regex rg = new Regex(@"\{(.|\s)*\}"); // find the json object from mixed content
        var json = rg.Match(data);

        return JsonConvert.DeserializeObject<TMessage>(json.Value);
    }
} 
希望这对将来的人有所帮助。但是,我的目标仍然是使用Simple.OData.Client或Microsoft.OData.Client

using (var httpClient = new HttpClient())
{
    using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://service-url/Entity/v1.3/$batch"))
    {
        request.Headers.TryAddWithoutValidation("Accept", "application/json");
        request.Headers.TryAddWithoutValidation("Client-Id", "XXXXXXXXX");  
        // Add all the headers here
 
        // this is your custom batch request
        request.Content = new StringContent("--batch\ncontent-type: multipart/mixed;boundary=changeset\n\n--changeset\ncontent-type: application/http\nContent-Transfer-Encoding: binary\n\nPOST CarEntries HTTP/1.1\ncontent-type: application/json;charset=utf-8\naccept: application/json;\n\n{\n\"RefId\": \"Test\",\n\"Child\": {\n\"ChildId\": \"412000415\"\n}\n}\n--changeset--\n--batch--");
        request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/mixed;boundary=batch"); // This is imporatnt - but please refer to your api documentation 

        var response = await httpClient.SendAsync(request);
        response.EnsureSuccessStatusCode();
        var multiPartContent = await response.Content.ReadAsMultipartAsync(); // This is part of the extension
        var mixedContent = multiPartContent.Contents.First(); // you will have multiple contents, select the content you want
        
        var data = await mixedContent.ReadAsStringAsync();// read it as string
        
        Regex rg = new Regex(@"\{(.|\s)*\}"); // find the json object from mixed content
        var json = rg.Match(data);

        return JsonConvert.DeserializeObject<TMessage>(json.Value);
    }
}