如何更新Json内容属性,如数组和其他子级别项?

时间:2018-03-21 13:17:29

标签: c# json json.net

我的目标:以编程方式更新数组属性和其他子json属性。

环境: Visual Studio C#,NewtonSoft Library

我有一个json文件样本,如下所示

LayoutInflater

我正在加载json文件内容,如下所示

TagViewUtils.getTag(view, ViewTag.VIEW_BACKGROUND.id) == R.drawable.twt_hover

我想更新别名属性,如下所示

首先我解析下面的数据以转换为对象

{
    "model": "xx.yyy",
    "make": "philips",  
    "alias": [ "abc", "bcd", "aee", "sample" ],
    "variables": {
        "temperature": {
            "dataType": "number",
            "unit": {
                "value": "°C",
                "enum": [ "°C", "°F" ]
            },
            "min": -50,
            "max": 300,
            "description": "The skin temperature measured outside of the motor."
        }
    }
}

从这里我可以更新根级别属性,如下所示。

var jsonContent = File.ReadAllText(@"\path\example.json");

我不确定如何更新变量<下的 别名 属性和 数据类型 属性/ em>的。我想要它如下

var dataObj = JObject.Parse(jsonContent); 

作为一种粗暴的方式,我尝试在序列化之后替换下面的值。

dataObj.Root["make"].Replace("siemens");

那也没有用。任何帮助。

  

注意:我希望看到解决方案而不关联类对象

2 个答案:

答案 0 :(得分:0)

此代码适用于我。我使用了这个json并将其反序列化为一个类对象。一旦你有了一个类对象,你就可以玩它并更新你想要的东西。

JSON:

{
   "model": "xx.yyy",
  "make": "philips",  
  "alias": [ "abc", "bcd", "aee", "sample" ],
"variables": {
"temperature": {
  "dataType": "number",
  "unit": {
    "value": "°C",
    "enum": [ "°C", "°F" ]
  },
  "min": -50,
  "max": 300,
  "description": "The skin temperature measured outside of the motor."
    }
}
}

C#类:

public class Unit
    {
        public string value { get; set; }
        public List<string> @enum { get; set; }
    }

    public class Temperature
    {
        public string dataType { get; set; }
        public Unit unit { get; set; }
        public int min { get; set; }
        public int max { get; set; }
        public string description { get; set; }
    }

    public class Variables
    {
        public Temperature temperature { get; set; }
    }

    public class RootObject
    {
        public string model { get; set; }
        public string make { get; set; }
        public List<string> alias { get; set; }
        public Variables variables { get; set; }
    }

C#代码:

var jsonContent = File.ReadAllText(@"C:\Users\kanaka\Desktop\sample.json");
var serializedData = JsonConvert.DeserializeObject<RootObject>(jsonContent);
var q = serializedData.variables.temperature.unit.@enum; // update whatever you want to, serialize again

现在,您可以将类对象值更新为您想要的任何值。更新后,再次序列化。如果将其反序列化为类对象

,它将更易于维护和轻松

答案 1 :(得分:0)

对于字符串数组属性别名,我已经实现了如下

int main(int argc, char* argv[])
{
    cv::Mat image = cv::imread("C:/StackOverflow/Input/coloredLines.png");

    cv::Mat mask = cv::Mat::zeros(image.size(), CV_8UC1);

    // create the edge mask:
    for (int j = 0; j < image.rows; ++j)
        for (int i = 0; i < image.cols; ++i)
            if (image.at<cv::Vec3b>(j, i) != cv::Vec3b(255, 255, 255)) mask.at<unsigned char>(j, i) = 255;


    // here's where your code starts:

    std::vector<std::vector<cv::Point > > contours;
    cv::findContours(mask, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);


    // now draw all the bounding rects, using  drawContours function:
    for (unsigned int i = 0; i < contours.size(); ++i)
    {
        cv::RotatedRect rect = cv::minAreaRect(contours[i]);
        cv::Point2f points[4];
        rect.points(points);

        // must be cv::Point to be used by drawContours function
        std::vector<cv::Point> boundingContour;

        // push all the contour points in that temporary vector
        for (unsigned int j = 0; j < 4; ++j)
            boundingContour.push_back(points[j]);

        // create a temporary dummy container that could hold multiple contours, but we'll only have exactly one in here
        std::vector<std::vector<cv::Point>> boxContours;
        boxContours.push_back(boundingContour);

        // there is only 1 contour inside, so always draw the 0-index contour!
        cv::drawContours(image, boxContours, 0, cvScalar(0, 255, 0), 2);

    }

    cv::imshow("image", image);

    cv::waitKey(0);
    return 0;
}

对于变量属性部分,我已经实现了如下

var aliasNew = new string[] { "abc", "bcd", "aee", "discard"};
var aliasSer = JsonConvert.SerializeObject(aliasNew);
dataObj.Root["alias"].Replace[aliasSer]