2017-12-06 5 views
-1

私はライセンスプレートを検出し、その情報を私に与えるアプリケーションに取り組んでいます。これは、 "openALPR"と呼ばれるRest APIを呼び出すことによって行われます。 その結果、私はこのように見てJSON文字列を取得:C#でJSONから値を抽出する

{ 
    "uuid": "", 
    "data_type": "alpr_results", 
    "epoch_time": 1512554543436, 
    "processing_time": { 
    "plates": 145.41024780273438, 
    "total": 147.6409999995667 
    }, 
    "img_height": 233, 
    "img_width": 700, 
    "results": [ 
    { 
     "plate": "MTF5101", 
     "confidence": 94.978622436523438, 
     "region_confidence": 0, 
     "vehicle_region": { 
     "y": 1, 
     "x": 234, 
     "height": 232, 
     "width": 232 
     }, 
     "region": "fr", 
     "plate_index": 0, 
     "processing_time_ms": 193.28457641601563, 
     "candidates": [ 
     { 
      "matches_template": 0, 
      "plate": "MTF5101", 
      "confidence": 94.978622436523438 
     } 
     ], 
     "coordinates": [ 
     { 
      "y": 102, 
      "x": 289 
     }, 
     { 
      "y": 101, 
      "x": 412 
     }, 
     { 
      "y": 126, 
      "x": 412 
     }, 
     { 
      "y": 128, 
      "x": 289 
     } 
     ], 
     "matches_template": 0, 
     "requested_topn": 50 
    }, 
    { 
     "plate": "MHV7718", 
     "confidence": 94.9754638671875, 
     "region_confidence": 0, 
     "vehicle_region": { 
     "y": 0, 
     "x": 395, 
     "height": 233, 
     "width": 233 
     }, 
     "region": "fr", 
     "plate_index": 1, 
     "processing_time_ms": 193.28457641601563, 
     "candidates": [ 
     { 
      "matches_template": 0, 
      "plate": "MHV7718", 
      "confidence": 94.9754638671875 
     }, 
     { 
      "matches_template": 0, 
      "plate": "MH7718", 
      "confidence": 81.550361633300781 
     }, 
     { 
      "matches_template": 0, 
      "plate": "MHY7718", 
      "confidence": 81.411224365234375 
     } 
     ], 
     "coordinates": [ 
     { 
      "y": 142, 
      "x": 475 
     }, 
     { 
      "y": 148, 
      "x": 676 
     }, 
     { 
      "y": 184, 
      "x": 674 
     }, 
     { 
      "y": 178, 
      "x": 475 
     } 
     ], 
     "matches_template": 0, 
     "requested_topn": 50 
    }, 
    { 
     "plate": "MTG7780", 
     "confidence": 94.97296142578125, 
     "region_confidence": 0, 
     "vehicle_region": { 
     "y": 1, 
     "x": 91, 
     "height": 232, 
     "width": 232 
     }, 
     "region": "fr", 
     "plate_index": 2, 
     "processing_time_ms": 193.28457641601563, 
     "candidates": [ 
     { 
      "matches_template": 0, 
      "plate": "MTG7780", 
      "confidence": 94.97296142578125 
     }, 
     { 
      "matches_template": 0, 
      "plate": "MT7780", 
      "confidence": 81.548896789550781 
     }, 
     { 
      "matches_template": 0, 
      "plate": "MT67780", 
      "confidence": 81.409553527832031 
     } 
     ], 
     "coordinates": [ 
     { 
      "y": 84, 
      "x": 18 
     }, 
     { 
      "y": 96, 
      "x": 202 
     }, 
     { 
      "y": 140, 
      "x": 201 
     }, 
     { 
      "y": 128, 
      "x": 16 
     } 
     ], 
     "matches_template": 0, 
     "requested_topn": 50 
    } 
    ], 
    "credits_monthly_used": 48, 
    "version": 2, 
    "credits_monthly_total": 2000, 
    "error": false, 
    "regions_of_interest": [], 
    "credit_cost": 1 
} 

今私はすべてのナンバープレートの座標を取得する必要があります。この例では、3つのナンバープレートがあります。各「座標」キーは4つの点(x、y)を含む。

これらの座標はどのようにC#で取得できますか?

私の質問をお寄せいただきありがとうございます!

よろしくお願いします。 Fabian Maurhart!手始めに

+0

[JSON応答の一部を抽出]の可能複製(https://stackoverflow.com/questions/15805175/extracting -part-of-json-response) – Hille

+0

json2csharpを使用してクラスを取得し、newtonsoft.jsonを使用して逆シリアル化します。 – Coder1409

+1

@ Coder1409興味のあるものがすべて値の場合でも、最初の部分を実行する必要がありますか?動的に、または「文字列型の」メソッド、newtonsoftのジェネリックJObjectクラスなどを介してアクセスできます。 –

答えて

0

は、このようなあなたのREST APIの出力のためのC#モデルクラスを作る:

enter image description here

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace JSONConversion.Models 
{ 

    public class LicensePlateInformation 
    { 
     public string uuid { get; set; } 
     public string data_type { get; set; } 
     public long epoch_time { get; set; } 
     public Processing_Time processing_time { get; set; } 
     public int img_height { get; set; } 
     public int img_width { get; set; } 
     public List<Result> results { get; set; } 
     public int credits_monthly_used { get; set; } 
     public int version { get; set; } 
     public int credits_monthly_total { get; set; } 
     public bool error { get; set; } 
     public object[] regions_of_interest { get; set; } 
     public int credit_cost { get; set; } 
    } 

    public class Processing_Time 
    { 
     public float plates { get; set; } 
     public float total { get; set; } 
    } 

    public class Result 
    { 
     public string plate { get; set; } 
     public float confidence { get; set; } 
     public int region_confidence { get; set; } 
     public Vehicle_Region vehicle_region { get; set; } 
     public string region { get; set; } 
     public int plate_index { get; set; } 
     public float processing_time_ms { get; set; } 
     public Candidate[] candidates { get; set; } 
     public List<Coordinate> coordinates { get; set; } 
     public int matches_template { get; set; } 
     public int requested_topn { get; set; } 
    } 

    public class Vehicle_Region 
    { 
     public int y { get; set; } 
     public int x { get; set; } 
     public int height { get; set; } 
     public int width { get; set; } 
    } 

    public class Candidate 
    { 
     public int matches_template { get; set; } 
     public string plate { get; set; } 
     public float confidence { get; set; } 
    } 

    public class Coordinate 
    { 
     public int y { get; set; } 
     public int x { get; set; } 
    } 
} 

した後、このようなGETリクエストをすることによって、あなたのAPIからあなたのJSONデータを取得します:

using System.Net.Http; 
using Newtonsoft.Json; 

public async Task<LicensePlateInformation> GetLicensePlateInfoFromAPI() 
{ 
    try 
    { 
    string stringifiedJSONFromAPI = string.Empty; 
    LicensePlateInformation plateInfo = new LicensePlateInformation(); 
    using(HttpClient client = new HttpClient()) 
    { 
     client.DefaultRequestHeaders.Accept.Clear(); 
     client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 

     HttpResponseMessage msg = await client.GetAsync($"<Insert rest api endpoint here>"); 

     if(msg.IsSuccessStatusCode) 
     { 
      stringifiedJSONFromAPI = await msg.Content.ReadAsStringAsync(); 
      JsonConvert.PopulateObject(stringifiedJSONFromAPI, plateInfo); 
     } 
     return plateInfo; 
    } 
    } 
    catch(Exception ex) 
    { 
     throw; 
    } 
} 

最後に、ちょうどすべてのあなたの詳細情報を取得するためにLINQを適用し、C#モデルクラスを取得した後:

using System.Linq; 
using System.Collections.Generic; 

LicensePlateInformation plateInfo = await GetLicensePlateInfoFromAPI(); 
List<Coordinate> coOrdinatesList = new List<Coordinate>(); 
foreach (var outerItem in plateInfo.results.Select(x => x.coordinates)) 
{ 
     foreach (var innerItem in outerItem) 
     { 
      coOrdinatesList.Add(innerItem); 
     } 
} 
関連する問題