如何将此类型的Json反序列化为C#对象

时间:2020-06-08 07:20:41

标签: c# json

"firstChainPrices":{
"Prices":[
{
"minPrice":"29250.00",
"currency":"KZT",
"brand":"DV.CITDMB.Y.3.OPT",
"minPriceAvailable":4,
"artificialPriceForChain":"29250.00"
},
{
"minPrice":"24950.00",
"currency":"KZT",
"brand":"DV.CITDMB.Y.1.LIGHT",
"minPriceAvailable":4,
"artificialPriceForChain":"24950.00"
},
{
"minPrice":"37350.00",
"currency":"KZT",
"brand":"DV.CITDMB.Y.5.FLEX",
"minPriceAvailable":4,
"artificialPriceForChain":"37350.00"
}
],
"Prices":[
{
"minPrice":"39118.00",
"currency":"KZT",
"brand":"DV.CITDMB.Y.3.OPT",
"minPriceAvailable":4,
"artificialPriceForChain":"39118.00"
},
{
"minPrice":"28148.00",
"currency":"KZT",
"brand":"DV.CITDMB.Y.1.LIGHT",
"minPriceAvailable":4,
"artificialPriceForChain":"28148.00"
},
{
"minPrice":"53643.00",
"currency":"KZT",
"brand":"DV.CITDMB.Y.5.FLEX",
"minPriceAvailable":4,
"artificialPriceForChain":"53643.00"
}
]

} 我想将此json反序列化为C#对象。但是此json包含不同的Price数组(在这种情况下为2),但是当我反序列化时,我只能得到一个Price数组。这不是我在这里给出的完整json(我删除了一些部分,因为json太长了)

1 个答案:

答案 0 :(得分:0)

虽然这不是最优雅的解决方案,但可以解决您的问题。 只需花点功夫,就可以根据需要进行概括。

 import numpy as np
import matplotlib.pyplot as plt
from testCases_v2 import *
import sklearn
import sklearn.datasets
import sklearn.linear_model
from planar_utils import plot_decision_boundary, sigmoid, load_planar_dataset, load_extra_datasets
from sklearn.metrics import accuracy_score

X, Y = load_planar_dataset()

plt.scatter(X[0, :], X[1, :], c=Y, s=40, cmap=plt.cm.Spectral);

plot_decision_boundary(lambda x: clf.predict(x), X, Y)
plt.title("Logistic Regression")

# Print accuracy
LR_predictions = clf.predict(X.T)
print(LR_predictions)
print ('Accuracy of logistic regression: %d ' % float((np.dot(Y,LR_predictions) + np.dot(1-Y,1-LR_predictions))/float(Y.size)*100) +
       '% ' + "(percentage of correctly labelled datapoints)")
score= accuracy_score(Y,LR_predictions)
print(score)



ValueError                                Traceback (most recent call last)
<ipython-input-8-0f3a55d2e963> in <module>()
      8 print ('Accuracy of logistic regression: %d ' % float((np.dot(Y,LR_predictions) + np.dot(1-Y,1-LR_predictions))/float(Y.size)*100) +
      9        '% ' + "(percentage of correctly labelled datapoints)")
---> 10 score= accuracy_score(Y,LR_predictions)
     11 print(score)

/opt/conda/lib/python3.6/site-packages/sklearn/metrics/classification.py in accuracy_score(y_true, y_pred, normalize, sample_weight)
    170 
    171     # Compute accuracy for each possible representation
--> 172     y_type, y_true, y_pred = _check_targets(y_true, y_pred)
    173     if y_type.startswith('multilabel'):
    174         differing_labels = count_nonzero(y_true - y_pred, axis=1)

/opt/conda/lib/python3.6/site-packages/sklearn/metrics/classification.py in _check_targets(y_true, y_pred)
     70     y_pred : array or indicator matrix
     71     """
---> 72     check_consistent_length(y_true, y_pred)
     73     type_true = type_of_target(y_true)
     74     type_pred = type_of_target(y_pred)

/opt/conda/lib/python3.6/site-packages/sklearn/utils/validation.py in check_consistent_length(*arrays)
    179     if len(uniques) > 1:
    180         raise ValueError("Found input variables with inconsistent numbers of"
--> 181                          " samples: %r" % [int(l) for l in lengths])
    182 
    183 

ValueError: Found input variables with inconsistent numbers of samples: [1, 400]

类似地,您可以考虑合并给定键下的所有列表。 要只有1个价目表。

欢呼

ps:推广此方法以更正同一部分中的任意重复键,需要您跟踪这些部分...这将是一个相当痛苦的过程。因此,如果只是价格引起了您的问题,而它没有在其他地方作为关键出现,那么这很容易解决;-)

ps2:哦,以防万一。。。我将每个“ Key”替换为“ Key0”,“ Key1”等...反序列化时,应编写一个代码来检查所有条目,并可能检查您的情况合并它们。