C#百度图片识别API调用返回数据包解析

百度图片识别api接口

  public static JObject GeneralBasic(string apikey,string secretkey,string path)
        {
            var client = new Baidu.Aip.Ocr.Ocr(apikey, secretkey);
            var image = File.ReadAllBytes(path);

            // 通用文字识别
            var result = client.GeneralBasic(image, null);
            return result;
        }

调用,方法,以及返回json 解析

     private void txbupload_Click(object sender, EventArgs e)
        {
            string apikey = txbapikey.Text;
            string secretkey = txbsercetkey.Text;
            string imagepath = txbpic.Text+ "\yancaoxuke.jpg";
            JObject result =   OcrDemo.GeneralBasic(apikey, secretkey, imagepath);          
                var txts = (from obj in (JArray)result.Root["words_result"]
                         //   let phrase = (JObject)obj["phrase"]
                            select (string)obj["words"]);
            foreach (var r in txts)
                Console.WriteLine(r);
       
        }

包结构:

log_id:3544105182,words_result_num:15,words_result:[
  {
    "words": "XX专卖零售许可证"
  },
  {
    "words": "许可证号xxx"
  },
  {
    "words": "XX有限公司"
  },
  {
    "words": "法定代表人xx"
  },
  {
    "words": "(负责人)"
  },
  {
    "words": "320507217865"
  },
  {
    "words": "企业型"
  },
  {
    "words": "有限责任公司"
  },
  {
    "words": "经營场xx室"
  },
  {
    "words": "卷烟,雪茄烟"
  },
  {
    "words": "许可范围"
  },
  {
    "words": "xxxx公司"
  },
  {
    "words": "供货单位"
  },
  {
    "words": "有效期服自"
  },

参考:1

const string s = "{"result" : "ok","tuc" : [ {"authors" : [ 13 ],"meaningId" : 2397164096556578585,"phrase" : {"text" : "机场","language" : "zho" }},{"authors" : [ 3266 ],"meaningId" : 1269040436618011315,"phrase" : {"text" : "機場","language" : "zho"}}]}";
var root = JObject.Parse(s);
var txts = (from obj in (JArray)root["tuc"]
            let phrase = (JObject)obj["phrase"]
            select (string)phrase["text"]);
foreach (var r in txts)
    Console.WriteLine(r);

2:

const string s = "{"result" : "ok","tuc" : [ {"authors" : [ 13 ],"meaningId" : 2397164096556578585,"phrase" : {"text" : "机场","language" : "zho" }},{"authors" : [ 3266 ],"meaningId" : 1269040436618011315,"phrase" : {"text" : "機場","language" : "zho"}}]}";
var root = JObject.Parse(s);
var txts = (from obj in (JArray)root["tuc"]
            select (string)obj["phrase"]["text"]);
foreach (var r in txts)
    Console.WriteLine(r);

如果你从未使用过 json.net,那么可以看这个帖子开始:http://blog.csdn.net/leftfist/article/details/38687745

http://bbs.csdn.net/topics/390869393

原文地址:https://www.cnblogs.com/zuochanzi/p/7152147.html