I am trying to deserialize multipart JSON message received from httpclient, but I have no experiences with the multipart message. The standard complex JSON is no problem thanks to newtonsoft, but I cant deserialize the multipart message. Is there a package or something for that. I dont want to create some complex code if no needed.
MESSAGE looks like:
*--------------------------70fc0426e01b4f98
Content-Disposition: form-data; name="event";
filename="0001_20220308194235_379109_event_5608_1642.json"
Content-Type: application/octet-stream*
{"packetCounter":"5608",
"capture_timestamp":"1646764948776",
"frame_timestamp":"0",
"capture_ts":"1646764948776000000",
"datetime":"20220308 194228776",
"plateText":"\u0042\u0041\u0030\u0030\u0036\u0046\u0056",
"plateUnicode":"\u0042\u0041\u0030\u0030\u0036\u0046\u0056",
"plateUTF8":"SL006FV",
"plateASCII":"SL006FV",
"plateCountry":"UK",
"plateConfidence":"0.672017",
"carState":"lost",
"roiID":"1",
"geotag":{"lat": 50.418114,"lon": 30.476213},"imageType": "plate","plateImageType": "png","plateImageSize": "0","carMoveDirection":"in",
"timeProcessing":"0",
"plateCoordinates":[959, 246, 132, 28],
"plateCoordinatesRelative":[0, 0, 0, 0],
"carID":"1642",
"GEOtarget":"Camera",
"imagesURI":["/local/fflprapp/tools.cgi?action=getImage&name=7/20220308194229_354996lp_SL006FV_0.jpg","/local/fflprapp/tools.cgi?action=getImage&name=5/20220308194228_891633roi_SL006FV_0.jpg"],
"imageFile":"/tmp/FFLPR_MMCR/images/5/20220308194228_891633roi_SL006FV_0.jpg",
"camera_info":{"SerialNumber":"000000000000","ProdShortName":"Mobotix M73","MACAddress":"0003C5200D41"},
"sensorProviderID":"23"
}
*--------------------------70fc0426e01b4f98
Content-Disposition: form-data; name="image"; filename="20220308194228_891633roi_SL006FV_0.jpg"
Content-Type: image/jpeg*
CodePudding user response:
thanks for the quick response. Maybe I wrotte it wrong way. The JSON message itself is OK, I use the following code for the Async client.
I try to link the file from the message and display datas from JSON and picture from this.
filename="20220308194228_891633roi_SL006FV_0.jpg" Content-Type: image/jpeg
But I am as far as this and cant link 2 messages/headers if it is the multipart message.
TCPServerAsync class:
internal class TCPServerAsync
{
private volatile bool stop = true;
public async Task StartAsync(int port)
{
var prefix = "http://*:12321/";
HttpListener listener = new HttpListener();
listener.Prefixes.Add(prefix);
try
{
listener.Start();
stop = false;
}
catch (HttpListenerException hlex)
{
return;
}
while (listener.IsListening)
{
var context = await listener.GetContextAsync();
try
{
await ProcessRequestAsync(context);
}
catch (Exception ex)
{
return;
}
if (stop == true) listener.Stop();
}
listener.Close();
}
public void Stop()
{
stop = true;
}
private async Task ProcessRequestAsync(HttpListenerContext context)
{
var body = await new StreamReader(context.Request.InputStream).ReadToEndAsync();
HttpListenerRequest request = context.Request;
byte[] b = Encoding.UTF8.GetBytes("ACK");
context.Response.StatusCode = 200;
context.Response.KeepAlive = false;
context.Response.ContentLength64 = b.Length;
var output = context.Response.OutputStream;
await output.WriteAsync(b, 0, b.Length);
context.Response.Close();
}
}
}
Deserialize class
public Rootobject DeSerializeJSON(string JSONmessage)
{
Rootobject obj = JsonConvert.DeserializeObject<Rootobject>(JSONmessage);
return obj;
}
public class Rootobject
{
public string packetCounter { get; set; }
public string capture_timestamp { get; set; }
public string frame_timestamp { get; set; }
public string capture_ts { get; set; }
public string datetime { get; set; }
public string plateText { get; set; }
public string plateUnicode { get; set; }
public string plateUTF8 { get; set; }
public string plateASCII { get; set; }
public string plateCountry { get; set; }
public string plateConfidence { get; set; }
public string carState { get; set; }
public string roiID { get; set; }
public Geotag geotag { get; set; }
public string imageType { get; set; }
public string plateImageType { get; set; }
public string plateImageSize { get; set; }
public string carMoveDirection { get; set; }
public string timeProcessing { get; set; }
public int[] plateCoordinates { get; set; }
public int[] plateCoordinatesRelative { get; set; }
public string carID { get; set; }
public string GEOtarget { get; set; }
public string[] imagesURI { get; set; }
public string imageFile { get; set; }
public Vehicle_Info vehicle_info { get; set; }
public Camera_Info camera_info { get; set; }
public string sensorProviderID { get; set; }
}
public class Geotag
{
public float lat { get; set; }
public float lon { get; set; }
}
public class Vehicle_Info
{
public string brand { get; set; }
public string model { get; set; }
public string type { get; set; }
public string color { get; set; }
public string confidence { get; set; }
}
public class Camera_Info
{
public string SerialNumber { get; set; }
public string ProdShortName { get; set; }
public string MACAddress { get; set; }
}
}
CodePudding user response:
You can use JsonConvert.DeserializeObject for this.
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string jsonText = File.ReadAllText(@"path\to\jsonFile.json");
JObject jsonObject = JsonConvert.DeserializeObject<JObject>(jsonText);
string packetCounter = jsonObject["packetCounter"].ToString();
}
}
}
In my example I'm getting the json text from a file. You just need to get it from the httpclient instead. Then you can deserialize it with JsonConvert.DeserializeObject
into a JObject
.