将一个json字符串添加到另一个作为子成员C# [英] Add one json string to another as child member C#

查看:87
本文介绍了将一个json字符串添加到另一个作为子成员C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个json字符串,如下所示

I have two json strings as shown below

{
  "account-Ind": "A",
  "multipage-Ind": " ",
  "relat-Ch-St-Nmbr": "0000001",
  "relat-Claim-Nmbr": "0000000",
  "schg-Remit-Flag": " ",
  "schg-Mgcare": "000000000",
  "schg-Co-Pay": "000000000",
  "schg-Deductible": "000000000",
  "schg-Co-Ins": "000000000",
  "schg-Payment": "000000000",
  "schg-Tot-Claim": "000000000",
  "schg-Elig": " ",
  "filler1": "                                                  "
}





这是第二个json字符串



and this is second json string

{
  "message-Code-1": "    ",
  "msg-Number-Pos1-1": " ",
  "msg-Number-Pos2-1": " ",
  "msg-Number-Pos3-1": " ",
  "message-Code-2": "    ",
  "msg-Number-Pos1-2": " ",
  "msg-Number-Pos2-2": " ",
  "msg-Number-Pos3-2": " ",
  "message-Code-3": "    ",
  "msg-Number-Pos1-3": " ",
  "msg-Number-Pos2-3": " ",
  "msg-Number-Pos3-3": " ",
  "message-Code-4": "    ",
  "msg-Number-Pos1-4": " ",
  "msg-Number-Pos2-4": " ",
  "msg-Number-Pos3-4": " "
}



我需要一个如下输出


I need an output like below

{
  "account-Ind": "A",
  "multipage-Ind": " ",
  "relat-Ch-St-Nmbr": "0000001",
  "relat-Claim-Nmbr": "0000000",
  "schg-Remit-Flag": " ",
  "schg-Mgcare": "000000000",
  "schg-Co-Pay": "000000000",
  "schg-Deductible": "000000000",
  "schg-Co-Ins": "000000000",
  "schg-Payment": "000000000",
  "schg-Tot-Claim": "000000000",
  "schg-Elig": " ",
  "filler1": "                                                  ",
  "message-Table":
    {
      "message-Code-1": "0000",
      "msg-Number-Pos1": "1",
      "msg-Number-Pos2": "1",
      "msg-Number-Pos3": "1",    
      "message-Code-2": "0001",
      "msg-Number-Pos1": "2",
      "msg-Number-Pos2": "1",
      "msg-Number-Pos3": "1",   
      "message-Code-3": "0002",
      "msg-Number-Pos1": "2",
      "msg-Number-Pos2": "1",
      "msg-Number-Pos3": "1",   
      "message-Code-4": "0004",
      "msg-Number-Pos1": "2",
      "msg-Number-Pos2": "1",
      "msg-Number-Pos3": "1", 
      "message-Code-5": "0005",
      "msg-Number-Pos1": "2",
      "msg-Number-Pos2": "1",
      "msg-Number-Pos3": "1"
    }
 
}

< br $> b $ b

我尝试过:





What I have tried:

var javaScriptSerializer = new JavaScriptSerializer();
                var userDetails = javaScriptSerializer.DeserializeObject(json1);
                var messageTable = javaScriptSerializer.DeserializeObject(json2);
                var arrayOfObjects = JsonConvert.SerializeObject(
                      new[] { JsonConvert.DeserializeObject(json1), JsonConvert.DeserializeObject(json2) });
                return arrayOfObjects;

But I getting result like below 

[
  {
    "account-Ind": "A",
    "multipage-Ind": " ",
    "relat-Ch-St-Nmbr": "0000001",
    "relat-Claim-Nmbr": "0000000",
    "record-Type": "1",
    "record-Seq": "0000",
    "cert-Nmbr": "109085006",
    "provider-Charge": "000035000",
    "int-Amount-Paid": "0000000",
    "clm-Account-Nmbr": "HNY1002     ",
    "alt-Id": "931929156",
    "schg-Elig": " ",
    "filler1": "                                                  "
  },
  {
    "message-Code-1": "    ",
    "msg-Number-Pos1-1": " ",
    "msg-Number-Pos2-1": " ",
    "msg-Number-Pos3-1": " ",
    "message-Code-2": "    ",
    "msg-Number-Pos1-2": " ",
    "msg-Number-Pos2-2": " ",
    "msg-Number-Pos3-2": " ",
    "message-Code-3": "    ",
    "msg-Number-Pos1-3": " ",
    "msg-Number-Pos2-3": " ",
    "msg-Number-Pos3-3": " ",
    "message-Code-4": "    ",
    "msg-Number-Pos1-4": " ",
    "msg-Number-Pos2-4": " ",
    "msg-Number-Pos3-4": " ",
    "message-Code-5": "    ",
    "msg-Number-Pos1-5": " ",
    "msg-Number-Pos2-5": " ",
    "msg-Number-Pos3-5": " "
  }
]

推荐答案

有几种方法可以达到这个目的。



第一种,就像你已经完成并返回一个对象数组。你显然需要知道数组中的对象是什么。

你已经在你的例子中隐式创建了两个对象的数组,所以这就是你得到的。



第二个和第三个意味着您需要对json对象进行反序列化,并以两种方式之一将第二个添加到第三个:



您可以添加sting,为您提供一个Json对象,使用字符串参数进行反序列化,该参数也需要反序列化。这种方法有它的用途,但我认为这不是你想要的。无论哪种方式,该过程类似于最后一个选项:



将对象1反序列化为一个类(您定义的)。其中一个类字段是将对象2反序列化为的对象(或字符串,见上文)。 IE:





There are a few ways to achieve this.

The first, is as you have done and return an array of objects. You would obviously need to know what object what what in the array.
You have implicitly created an array of the two objects in your example so this is what you get.

The second and third would mean that you need to de-serialize the json object(s) and add the second to the third in one of two ways:

You can either add the sting, giving you a Json object to be deserialized with a string parameter that also needs to be deserialized. This method has it's uses, but I don't think that's what you want. Either way, the process is similar to the last option:

Deserialize object 1 into a class (that you define). One of the class fields is an object (or string, see above) to deserialize object 2 into. I.E.:


class Object1 {
    [JsonProperty("account-Ind")]
    public string AccountInd {get;set;}
    [JsonProperty("multipage-Ind")]
    public string MultipageInd {get;set;}
    [JsonProperty("relat-Ch-St-Nmbr")]
    public string RelatChStNmbr {get;set;}
    [JsonProperty("relat-Claim-Nmbr")]
    public string RelatClaimNmbr {get;set;}
    [JsonProperty("record-Type")]
    public string RecordType {get;set;}
    [JsonProperty("record-Seq")]
    public string RecordSeq {get;set;}
    [JsonProperty("cert-Nmbr")]
    public string CertNmbr {get;set;}
    [JsonProperty("provider-Charge")]
    public string ProviderCharge {get;set;}
    [JsonProperty("int-Amount-Paid")]
    public string IntAmountPaid {get;set;}
    [JsonProperty("clm-Account-Nmbr")]
    public string ClmAccount-Nmbr {get;set;}
    [JsonProperty("alt-Id")]
    public string AltId {get;set;}
    [JsonProperty("schg-Elig")]
    public string SchgElig {get;set;}
    [JsonProperty("filler1")]
    public string Filler1 {get;set;}
    [JsonProperty("message-Table")]
    public Object2[] MessageTable {get;set;}
}


var javaScriptSerializer = new JavaScriptSerializer();
                var userDetails = javaScriptSerializer.DeserializeObject<Object1>(json1);
                var messageTable = javaScriptSerializer.DeserializeObject<Object2>(json2);
                userDetails.MessageTable = messageTable;
                return userDetails;  //or JavaScriptSerializer.Serialize(userDetails);







我没有指定Object2,你明白了。



我指定MessageTable应该是一个数组。如果你在同一个对象中有多个具有相同名称的字段,我认为你会遇到问题



我希望这足以让你顺利上路



Andy ^ _ ^




I didn't specify Object2, you get the idea.

I specified that MessageTable should be an array. I think you'll have issues if you have multiple fields with the same name in the same object

I hope that's enough to get you on your way

Andy ^_^


鉴于你无法反序列化成一个类(由于某种未知的原因),试试这个hack:



Given that you can't deserialize into a class (for some unknown reason), try this hack:

var result = json1.Insert(json1.indexOf("}"),json2)


这篇关于将一个json字符串添加到另一个作为子成员C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆