如何连接2个JSON字符串..? [英] How to concatenate 2 JSON strings..?

查看:277
本文介绍了如何连接2个JSON字符串..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好有..

我想连接两个json字符串。

第一个字符串包含数据表记录详细信息,其他字符串包含Count值。

我得到的值在数据集中这样。



数据集ds.Tables [0] = //表记录

ds .tables [1] = //单数整数值(例如TotalCount = 12)



我在json中转换ds但是它返回json字符串。

 {contactList:[{姓名:Abhijit Goswami,电子邮件:abhijitda@facebook.com,contactNo:9158417355,性别:男性},{姓名:Abhijit Goswami,电子邮件:abhijitda @ facebook.com,contactNo:9158417355,性别:男性},{姓名:Abhijit哥斯瓦米, 电子邮件: abhijitda@facebook.com, contactNo: 9158417355, 性别: 男性}], TOTALCOUNT:[{ 总计:192}]} 





在上面的字符串中我不想在数组中使用totalCount值。

I只想在联系人列表数组之后totalCount:192。



 {contactList:[{姓名:Abhijit Goswami,电子邮件:abhijitda@facebook.com,contactNo:9158417355,性别:男性} ,{姓名:Abhijit Goswami,电子邮件:abhijitda @ facebook.com,contactNo:9158417355,性别:男性}],totalCount:192} 





我尝试了什么:



目前是我的用于将DataSet转换为JSON字符串的代码如下所示


 ds.Tables [0] .TableName =contactList; 
ds.Tables [1] .TableName =totalCount;
json = JsonConvert.SerializeObject(ds,Formatting.None);

解决方案

尝试这样的事情:

 json = JsonConvert.SerializeObject( new  
{
contactList = ds.Tables [ 0 ],
totalCount = ds.Tables [ 1 ]。Rows.Cast< DataRow>() 。选择(r = > r [ 0 ])。FirstOrDefault()
}, Formatting.None);


Hello there's..
I want to concatenate two json string.
first string contains data tables records details and other string contains Count value.
I get that values In data set such that.

dataset ds.Tables[0]= //table records
ds.tables[1]=// singale integer value (e.g. TotalCount=12)

I convert ds in json but it return json string like.

{"contactList":[{"Name":"Abhijit Goswami","email":"abhijitda@facebook.com","contactNo":"9158417355","gender":"Male"},{"Name":"Abhijit Goswami","email":"abhijitda@facebook.com","contactNo":"9158417355","gender":"Male"},{"Name":"Abhijit Goswami","email":"abhijitda@facebook.com","contactNo":"9158417355","gender":"Male"}],"totalCount":[{"Total":192}]}



In above string I don't want to totalCount value in array.
I just want to "totalCount:192" after the contact list array.

{"contactList":[{"Name":"Abhijit Goswami","email":"abhijitda@facebook.com","contactNo":"9158417355","gender":"Male"},{"Name":"Abhijit Goswami","email":"abhijitda@facebook.com","contactNo":"9158417355","gender":"Male"}],"totalCount":192}



What I have tried:

currently my code for converting DataSet to JSON string as bellow

ds.Tables[0].TableName = "contactList";
        ds.Tables[1].TableName = "totalCount";             
        json = JsonConvert.SerializeObject(ds, Formatting.None);

解决方案

Try something like this:

json = JsonConvert.SerializeObject(new 
{
    contactList = ds.Tables[0],
    totalCount = ds.Tables[1].Rows.Cast<DataRow>().Select(r => r[0]).FirstOrDefault()
}, Formatting.None);


这篇关于如何连接2个JSON字符串..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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