0

I have a json data like below:

{
"ResourceStrings": 
   [
    {
        "StringKey": "TestKey",
        "StringID": 1,
        "Value": "This translate need to be done123fdff"
    }, 
    {
        "StringKey": "SampleKey",
        "StringID": 2,
        "Value": "This translate need to be done345fdfd"
    }

]
}

I converted this to class so below the created class:

public class ResourceString
{
    public string StringKey { get; set; }
    public int StringID { get; set; }
    public string Value { get; set; }
}
public class RootObject
{
    public List<ResourceString> ResourceStrings { get; set; }
}

Now i have some data in xliff format which i am taking from this element and updating in the Rootobject like below:

     XmlDocument docXLIFF = new XmlDocument();
    docXLIFF.LoadXml(xliffdata);
    var jsondata = JsonConvert.DeserializeObject<RootObject>      (sameJsonDataAsAbove);
    List<ResourceString> rstList = jsondata.ResourceStrings.ToList();

    XmlNodeList xmlNodes = docXLIFF.SelectNodes("/xliff/file/body/trans-unit");
    foreach (XmlNode node in xmlNodes)
    {
        var getTransID = rstList.Where(t => t.StringID.ToString() == node.Attributes["id"].Value).FirstOrDefault();
        if (getTransID != null)
        {
            var getTargetValue = node.InnerText;
            getTransID.Value = getTargetValue;
        }
    }

So in the above code i am checking the id of xliff element with jsonList id and it's match i am getting the value of element and setting in the json list. after completing this i need the updated json list in the same json form as i mentioned above. But i am getting only the content like below:

    {
        "StringKey": "TestKey",
        "StringID": 1,
        "Value": "This translate need to be done123fdff"
    }, {
        "StringKey": "SampleKey",
        "StringID": 2,
        "Value": "This translate need to be done345fdfd"
    }

How to achieve the same json structure?

Viplock
  • 3,259
  • 1
  • 23
  • 32
Vikash
  • 340
  • 1
  • 5
  • 24

1 Answers1

1

I think you need to parse the string into an array :-)

See this post for more help:

Parse this json string to string array c#

Edit: sorry Liam, here you go

    //add [] to the string so it's an array!
var xcliff = " [ { " +
    "\"StringKey\": \"TestKey\"," +
    "\"StringID\": 1," +
    "\"Value\": \"This translate need to be done123fdff\"" +
"}, {" +
    "\"StringKey\": \"SampleKey\"," +
    "\"StringID\": 2," +
    "\"Value\": \"This translate need to be done345fdfd\"" +
"}]";

var jd = new JavaScriptSerializer().Deserialize<List<ResourceString>>(xcliff);
var ro = new RootObject { ResourceStrings = jd};
Community
  • 1
  • 1
Bert Persyn
  • 401
  • 4
  • 13
  • Liam i added this code after ending foreach loop var convertToJson = JsonConvert.SerializeObject(rstList); but i am getting the content only it should come with ResourceString array also. – Vikash Oct 21 '16 at 08:05
  • Like this i should get back with updated value { "ResourceStrings": [ { "StringKey": "TestKey", "StringID": 1, "Value": "updated value" }, { "StringKey": "SampleKey", "StringID": 2, "Value": "updated value" } ] } – Vikash Oct 21 '16 at 09:04
  • 1
    Yes Bert your code was right after using your tricky code i serialize it and it is working fine the same i wanted to achieve thanks once again i am marking it as answer. – Vikash Oct 21 '16 at 09:09