RestSharp C#JsonDeserializer要列出的对象属性值设置为null

时间:2018-07-07 19:55:33

标签: c# restsharp

我的响应对象中有一个json结果集,如下所示

select group_concat(pvid_id separator ', ') as result
  from tab
 where tag_id = 1 -- or tag_id = 2

result
 1, 2

以及以下课程

 [{
"id": "13",
"category_id": "[\"2\"]",
"store_id": "[\"3\"]",
"name": "Sp juise",
"price": "1",
"description": "<p><b><\/b><br><\/p>",
"image": "<p>You did not select a file to upload.<\/p>",
"active": "1"
}, {
"id": "12",
"category_id": "[\"2\"]",
"store_id": "[\"3\"]",
"name": "Sp juise",
"price": "1",
"description": "<p><b><\/b><br><\/p>",
"image": "<p>You did not select a file to upload.<\/p>",
"active": "1"
}, {
"id": "11",
"category_id": "[\"3\"]",
"store_id": "[\"2\"]",
"name": "Berger (Chicken)",
"price": "80",
"description": "<p>Chicken Berger<\/p>",
"image": "assets\/images\/product_image\/5b374e99c53ce.jpg",
"active": "1"
 }, {
"id": "10",
"category_id": "[\"2\"]",
"store_id": "[\"2\"]",
"name": "Juce",
"price": "2",
"description": "<p>mixed<\/p>",
"image": "<p>You did not select a file to upload.<\/p>",
"active": "1"
 }, {
"id": "9",
"category_id": "null",
"store_id": "null",
"name": "Orenge juse",
"price": "2.5",
"description": "",
"image": "assets\/images\/product_image\/5b374ece3d909.jpg",
"active": "1"

 }]

以下用于从json获取产品列表的代码

 public class Product
 {
    public string id;
    public string category_id;
    public string store_id;
    public string name;
    public string price;
    public string description;
    public string image;
    public string active;

}

产品具有可以枚举的对象,但是这些字段未映射到类属性,并且它们对于列表中的每个对象都返回空值。

1 个答案:

答案 0 :(得分:0)

这是由于缺少获取和设置 下面解决了这个问题

/***************************************************************************
 *   Copyright (C) 2018 by Paul Lutus                                      *
 *   lutusp@arachnoid.com                                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

// classic Gauss-Jordan matrix manipulation functions

var gj = gj || {}

gj.divide = function(A,  i,  j,  m) {
  for (var q = j + 1; q < m; q++) {
    A[i][q] /= A[i][j];
  }
  A[i][j] = 1;
}

gj.eliminate = function(A, i, j, n, m) {
  for (var k = 0; k < n; k++) {
    if (k != i && A[k][j] != 0) {
      for (var q = j + 1; q < m; q++) {
        A[k][q] -= A[k][j] * A[i][q];
      }
      A[k][j] = 0;
    }
  }
}

gj.echelonize = function(A) {
  var n = A.length;
  var m = A[0].length;
  var i = 0;
  var j = 0;
  var k;
  var swap;
  while (i < n && j < m) {
    //look for non-zero entries in col j at or below row i
    k = i;
    while (k < n && A[k][j] == 0) {
      k++;
    }
    // if an entry is found at row k
    if (k < n) {
      //  if k is not i, then swap row i with row k
      if (k != i) {
        swap = A[i];
        A[i] = A[k];
        A[k] = swap;
      }
      // if A[i][j] is != 1, divide row i by A[i][j]
      if (A[i][j] != 1) {
        gj.divide(A, i, j, m);
      }
      // eliminate all other non-zero entries
      gj.eliminate(A, i, j, n, m);
      i++;
    }
    j++;
  }
}

// a simple data class

function Pair(x,y) {
  this.x = x;
  this.y = y;
};

Pair.prototype.toString = function() {return x + ',' + y};

// matrix functions

var matf = matf || {}

// a weak substitue for printf()

matf.number_format = function(n,p,w) {
  s = n.toExponential(p);
  while(s.length < w) {
    s = ' ' + s;
  }
  return s;
}

// produce a single y result for a given x

matf.regress = function(x, terms) {
  var y = 0;
  var m = 1;
  for (var i = 0; i < terms.length;i++) {
    y += terms[i] * m;
    m *= x;
  }
  return y;
}

// compute correlation coefficient

matf.corr_coeff = function(data, terms) {
  var r = 0;
  var n = data.length;
  var sx = 0;
  var sx2 = 0, sy = 0, sy2 = 0, sxy = 0;
  var x, y;
  for (var i = 0;i < data.length;i++) {
    pr = data[i];
    var x = matf.regress(pr.x, terms);
    var y = pr.y;
    sx += x;
    sy += y;
    sxy += x * y;
    sx2 += x * x;
    sy2 += y * y;
  }
  var div = Math.sqrt((sx2 - (sx * sx) / n) * (sy2 - (sy * sy) / n));
  if (div != 0) {
    r = Math.pow((sxy - (sx * sy) / n) / div, 2);
  }
  return r;
}

// compute standard error

matf.std_error = function(data, terms) {
  var  r = 0;
  var  n = data.length;
  if (n > 2) {
    var a = 0;
    for (var i = 0;i < data.length;i++) {
      pr = data[i];
      a += Math.pow((matf.regress(pr.x, terms) - pr.y), 2);
    }
    r = Math.sqrt(a / (n - 2));
  }
  return r;
}


// create regression coefficients
// for provided data set
// data = pair array
// p = polynomial degree

matf.compute_coefficients = function(data,  p) {
  p += 1;
  var n = data.length;
  var r, c;
  var rs = 2 * p - 1;
  //
  // by request: read each datum only once
  // not the most efficient processing method
  // but required if the data set is huge
  //
  // create square matrix with added RH column
  m = Array();
  for (var i = 0; i < p; i++) {
    mm = Array();
    for (var j = 0; j <= p; j++) {
      mm[j] = 0;
    }
    m[i] = mm;
  }
  //double[][] m = new double[p][p + 1];
  // create array of precalculated matrix data
  mpc = Array();
  for(var i = 0;i < rs;i++) {
    mpc[i] = 0;
  }
  mpc[0] = n;
  for (var i = 0;i < data.length;i++) {
    pr = data[i];
    // process precalculation array
    for (r = 1; r < rs; r++) {
      mpc[r] += Math.pow(pr.x, r);
    }
    // process RH column cells
    m[0][p] += pr.y;
    for (r = 1; r < p; r++) {
      m[r][p] += Math.pow(pr.x, r) * pr.y;
    }
  }
  // populate square matrix section
  for (r = 0; r < p; r++) {
    for (c = 0; c < p; c++) {
      m[r][c] = mpc[r + c];
    }
  }
  // reduce matrix
  gj.echelonize(m);
  // extract result column
  terms = Array();
  for (var i = 0;i < m.length;i++) {
    mc = m[i];
    terms[i] = mc[p];
  }
  return terms;
}

// test the system using known data

matf.test = function() {
  var xd = [-1,0,1,2,3,5,7,9];
  var yd = [-1,3,2.5,5,4,2,5,4];
  
  data = Array();
  
  for(var i = 0;i < xd.length;i++) {
    data[i] = new Pair(xd[i],yd[i]);
  }
  
  terms = compute_coefficients(data,6);
  
  var prec = 16;
  
  var width = 24;
  
  for(var i = 0;i < terms.length;i++) {
    print(number_format(terms[i],prec,width) + ' * x^' + i);
  }
  
  cc = corr_coeff(data,terms);
  
  print ('cc = ' + number_format(cc,prec,width));
  
  se = std_error(data,terms);
  
  print('se = ' + number_format(se,prec,width));
}

//test();

// "data" is an array of Pair(x,y) data
// p = polynomial degree

matf.process_data = function(data,p) {
  var terms = matf.compute_coefficients(data,p);
  var cc = matf.corr_coeff(data,terms);
  var se = matf.std_error(data,terms);
  return [terms,cc,se];
}

/**** END Paul Lutus' code ****/


function f(cs, x){
  let n = cs.length - 1;
  let result = 0;
  for (let i=0; i<cs.length; i++)
    result += cs[i] * Math.pow(x, i);
  return result;
}

var data = [[1,1], [5,2], [20,3], [50,4], [100,5]];
var xy_data = []

for (let i of data)
  xy_data.push(new Pair(i[0], i[1]));

var result = matf.process_data(xy_data, xy_data.length - 1);

for (let i=0; i<data.length; i++)
  console.log(data[i][0], f(result[0], data[i][0]));