I have a requirement to get the first set of records in c# JSON. Here is my JSON data for example
string sJSON = "{
{
"ID": "1",
"Name":"John",
"Area": "Java" ,
"ID": "2",
"Name": "Matt",
"Area": "Oracle" ,
"ID": "3","Name":
"Danny","Area": "Android"
}
}"
I need to extract only the 1st set of records (i.e. {{"ID": "1", "Name":"John", "Area": "Java"}}).
If there is only one record my code (see below) works fine but when there are multiple records it takes the last set of JSON data (i.e. {{"ID": "3","Name": "Danny","Area": "Android"}}).
JObject jsonObject = JObject.Parse(sJSON);
string sID = (string)jsonObject["ID"];
string sName = (string)jsonObject["Name"];
string sArea = (string)jsonObject["Area"];
Can anyone help me extract only the 1st set of JSON data?
You return the JSON data as JSON Array, now it is a simple JSON string.
If you're able to generate like below then you can easily get the single JSON element easily.
[{ "ID": "1", "Name":"John", "Area": "Java" },{ "ID": "2","Name": "Matt","Area": "Oracle" },{ "ID": "3","Name": "Danny","Area": "Android" } ]
Use the below link, to understand this JSON format.
http://json.parser.online.fr/
Parse the data as JToken.
Such that you have something like:
var jToken = JToken.Parse(sJSON);
var isEnumerable = jToken.IsEnumerable()
if(isEnumeable)
{
cast as JArray and get firstOrdefault
}
var isObject = jToken.IsObject();
if(isObject )
{
cast as JObject and perform straight casting
}
Related
How to get json object using path in string variable,This is the JSON Schema data structure string stored in my database,I tried to convert to json data structure but it didn't work,I want to convert such a string data structure into a json object.
string json = $#"{{
""xcollapse.id"":""xcollapse"",
""xcollapse.el"":202,
""xcollapse.cols.props.cn"":""title"",
""xcollapse.cols.props.desc"":"""",
""xcollapse.cols.props.visible"":-1,
""xcollapse.cols.children.id"":""f272681"",
""xcollapse.cols.children.el"":200,
""xcollapse.cols.children.props"":""{{}}""
}}";
var jObj = JObject.Parse(json);
var result = jObj.ToString();
I would like to get the following results.
{
"xcollapse": {
"id": "xcollapse",
"el": "202",
"cols": [
{
"props": {
"cn": "title",
"desc": "",
"visible": "-1"
},
"children": {
"id": "title",
"el": "",
"props": [
{}
]
}
}
]
}
}
The complete data structure in the database looks like this,I now need to convert to json objects to return to the front end.
{
"xcollapse.id":"xcollapse",
"xcollapse.el":202,
"xcollapse.props":{
},
"xcollapse.styles.visible":-1,
"xcollapse.cols.deletable":false,
"xcollapse.cols.props.cn":"title",
"xcollapse.cols.props.desc":"",
"xcollapse.cols.props.visible":-1,
"xcollapse.cols.styles":null,
"xcollapse.cols.children.id":"f272681",
"xcollapse.cols.children.el":200,
"xcollapse.cols.children.props":{
},
"xcollapse.cols.children.styles.visible":-1,
"xcollapse.cols.children.cols.deletable":false,
"xcollapse.cols.children.cols.props.visible":-1,
"xcollapse.cols.children.cols.styles.span":8,
"xcollapse.cols.children.cols.children.id":"fname",
"xcollapse.cols.children.cols.children.el":100,
"xcollapse.cols.children.cols.children.props.ek":"fbillhead",
"xcollapse.cols.children.cols.children.props.cn":"title",
"xcollapse.cols.children.cols.children.props.group":"",
"xcollapse.cols.children.cols.children.props.fn":"fname",
"xcollapse.cols.children.cols.children.props.pn":"fname",
"xcollapse.cols.children.cols.children.props.desc":"",
"xcollapse.cols.children.cols.children.props.must":"0",
"xcollapse.cols.children.cols.children.props.visible":-1,
"xcollapse.cols.children.cols.children.props.len":"100",
"xcollapse.cols.children.cols.children.props.lock":"0",
"xcollapse.cols.children.cols.children.props.copy":"1",
"xcollapse.cols.children.cols.children.props.defval":"",
"xcollapse.cols.children.cols.children.props.canchange":"0",
"xcollapse.cols.children.cols.children.props.xlsin":"1",
"xcollapse.cols.children.cols.children.props.acl":"1",
"xcollapse.cols.children.cols.children.props.sbm":"0",
"xcollapse.cols.children.cols.children.props.notrace":"1",
"xcollapse.cols.children.cols.children.props.apipn":"sourceType",
"xcollapse.cols.children.cols.children.props.ctlfk":"",
"xcollapse.cols.children.cols.children.props.editmode":"0",
"xcollapse.cols.children.cols.children.props.xsslv":"0",
"xcollapse.cols.children.cols.children.styles.align":"left",
"xcollapse.cols.children.cols.children.styles.width":"",
"xcollapse.cols.children.cols.children.styles.row":2,
"xcollapse.cols.children.cols.children.cols":{},
"xcollapse.cols.children.cols.children.deletable":true,
"xcollapse.cols.children.cols.children.pid":"",
"xcollapse.cols.children.cols.children.seq":1,
"xcollapse.cols.children.cols.seq":1,
"xcollapse.cols.children.deletable":true,
"xcollapse.cols.children.pid":"",
"xcollapse.cols.children.seq":1,
"xcollapse.cols.seq":1,
"xcollapse.deletable":true,
"xcollapse.pid":"",
"xcollapse.seq":1,
}
I want to create a method with the C# Newtonsoft library that can take in a parameter value to return the JSON data value, without needing to know and create a class beforehand, but all I can find are examples to deserialise into a class or into a dynamic object, both needing to know JSON structure prior at development time
Here's an example of what the kind of JSON format I'm expecting, but is subject to change:
{
"Input":
[
{
"Name": "foo"
},
{
"Name": "bar"
},
]
"Output":
[
{
"Name": "bob"
},
{
"Name": "builder"
},
]
}
I'm locked into using Newtonsoft library to work on the JSON file, or do it myself from scratch as it's an embedded system.
You can use JObject. If you deserialize a class without the type it will be deserialized to JObject. You would access your JObject values with named index which is obviously your property name. Other type of interest to you is JArray. This all resides in namespaces:
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Example:
So the example with your JSON would be:
var json = #"{
""Input"":
[
{ ""Name"": ""foo"" },
{ ""Name"": ""bar"" }
],
""Output"":
[
{ ""Name"": ""bob"" },
{""Name"": ""builder""}
]
}";
var obj = JsonConvert.DeserializeObject(json) as JObject;
var input = obj["Input"] as JArray;
var inputArray = input[0]["Name"];
This would get the first element in array that is in Input field - "foo".
I am converting XML to JSON.
Input:
<emp
id="17377"/>
<CustomerList>
<Customer
id="67149"/>
<Customer id="64260"/>
</CustomerList>
OutPut:
"emp": {
"id": "17377"
},
"CustomerList": {
"Customer": [
{
"id": "67149"
},
{
"id": "64260"
}
]
}
But I need the below output. But I can not remove <Customer from <CustomerList> in the input. Also Please note that I need accept dynamic name
of array input. But always i want to remove the inner property name to be removed. in this example its Customer.But I may get MarkList->Mark then I need to remove remove Mark, etc.,:
"emp": {
"id": "17377"
},
"CustomerList": [
{
"id": "67149"
},
{
"id": "64260"
}
]
Is this possible please.
I use below code to convert XML to Json:
var xml = new XmlDocument();
xml.XmlResolver = null;
xml.LoadXml(richTextBox1.Text);
var jsonText = JsonConvert.SerializeXmlNode(xml,Newtonsoft.Json.Formatting.Indented);
Note:
One solution would be find the char "[" and remove before "[" and after "{".
This is not possible, as it is simply trying to change to JSON scheme in which it was orignally built.
what you can do, is use JObject to change the value of customer to feet your needs:
JObject rss = JObject.Parse(json);
JObject customers = rss.SelectToken("CustomerList");
customers ["Customer"] = newValue;
This is the snippet, modify this in your code to feet your needs.
I am trying to create JsonObject with the below-mentioned structure.
{
"id": "1",
"name": "XXX",
"age": "30"
}
Using the code,
dynamic sampleJson = new JObject();
sampleJson.Add("id", "1");
sampleJson.Add("name", "XXX");
sampleJson.Add("age", "30");
But the problem is that extra curly braces is appearing at the begining and end of the json structure, as shown below.
{{
"id": "1",
"name": "XXX",
"age": "30"
}}
I use the required JSON structure as the post body of an API and it should be in the JSON format(So cant use JSON string structure using ToString() method).How can I remove the extra braces and achieve my requirement ???
Since you are using a JObject, you can simply call the ToString() override to create your JSON. For example:
JObject sampleJson = new JObject();
sampleJson.Add("id", "1");
sampleJson.Add("name", "XXX");
sampleJson.Add("age", "30");
var json = sampleJson.ToString();
Now your json variable will contain:
{
"id": "1",
"name": "XXX",
"age": "30"
}
ToString() didn't work for me. The following did:
JsonConvert.DeserializeObject(JSONStringHere, typeof(ExpandoObject));
Lets say i have the following JSON
{
"data": [
{
"from": {
"name": "aaa bbb",
},
"actions": [
{
"name": "Comment",
"link": "http://...
},
{
"name": "Like",
"link": "http://.."
}
],
},
And i have
JSONObject wallData = helper.Get("/me/feed");
if (wallData != null)
{
var data = wallData.Dictionary["data"];
List<JSONObject> wallPosts = data.Array.ToList<JSONObject>();
}
foreach (Facebook.JSONObject wallItem in wallPosts)
{ ... }
Which stores me whole feed into wallData and 'data' object into wallPosts.
So then i can access the wallItem.Dictionary["from"].Dictionary["name"], and i get "aaa bbb".
But i can't get inside the actions array
The wallItem.Dictionary["actions"].Dictionary["name"] doesn't work.
Any idea
You need to do something like wallItem.Dictionary["actions"][0].Dictionary["name"] because "actions" is an array.
On a different note...its neater if u directly into a class...like this
var jSerializer = new JavaScriptSerializer();
var jsonObject = jSerializer.Deserialize<DataObject>(json);
The DataObject will be a class which emulates ur JSON data in a strongly typed class. Depending on the size of ur Json you will not have to use a lot of strings in your code.