How to convert this json string to Jobject
"{\"user\":{\"id\":373697,\"username\":\"luckygirlxxx123\",\"counter_rechecking\":0,\"user_id\":76131,\"fb_id\":\"100047460285611\"}}";
I need it convert to Jobject like this because i need it to post data to website api . Example :
JObject concac = new JObject();
concac1["id"] = 373697;
concac1["username"] = "luckygirlxxx123";
concac1["counter_rechecking"] = 0;
concac1["user_id"] = 76131;
concac1["fb_id"] = "100047460285611";
Because It inside USER that i dont know how to create jobject with that
this code might help you.
public static void Main(string[] args)
{
string jsonData = "{\"user\":{\"id\":373697,\"username\":\"luckygirlxxx123\",\"counter_rechecking\":0,\"user_id\":76131,\"fb_id\":\"100047460285611\"}}";
var details = JObject.Parse(jsonData);
Console.WriteLine(string.Concat(details["user"]["id"], " " + details["user"]["username"]));
}
The simplest way is by giving those JSON values to the contracture of the object
public JObject(int id, string username, int user_id ....)
then you declare object and you pass values to it
JObject concac = new JObject(concac1["id"], concac1["username"], concac1["user_id"]..... );
The second way is JSON Serialization And Deserialization In C#
Related
How can I take just the first element from a Json?
//take result back
void HandleIncomingMessage(object sender, PEventArgs e)
{
RetMessage += e.Result;
//here can not deserialize correct
var deserialized_message = JsonConvert.DeserializeObject<Message>(RetMessage);
}
Here I am doing the deserialize but because it`s taking the entire object can not parse it correct.
I need just JSON.[0]
Edit: Raw Json :
[{"unique_id":55,"action_name":"INSERT","start_date":"2018-06-11T16:00:00","end_date":"2018-06-11T17:00:00"},"1sddsd","my_channel"]
Deserialize to List<dynamic>, then read the properties of its first element.
//using Newtonsoft.Json;
var input = #"[{""unique_id"":55,""action_name"":""INSERT"",""start_date"":""2018-06-11T16:00:00"",""end_date"":""2018-06-11T17:00:00""},""1sddsd"",""my_channel""]";
var output = JsonConvert.DeserializeObject<List<dynamic>>(input);
Console.WriteLine(output[0].unique_id);
Output:
55
DotNetFiddle
How about getting json string and using JSON.net
//first create object from json
JObject jObject = JObject.Parse(jsonString);
//read unique value
string jUniqueId = jObject["unique_id"];
//or
string firstObject = jObject[0];
the solution is for static,
JObject obj= JObject.Parse(here pass the JSON);
string id= JSON[0]["unique_id"],
string name= JSON[0]["action_name"],
string sdate= JSON[0]["start_date"],
string edate= JSON[0]["end_date"]
dynamic means pass i instead of 0.
I need to add a Key for the Orphan JSON object
JSON String:
string jsonString = "{\"FirstName\":\"Emma\",\"LastName\":\"Watson\"}";
Expected JSON String:
string jsonString = "{\"PersonName\":{\"FirstName\":\"Emma\",\"LastName\":\"Watson\"}}";
I need to add a Key for the above said actual JSON string as like expected JSON using C#.
I tried the following code:
string rootKey = "PersonName";
string jsonString = "{\"FirstName\":\"Emma\",\"LastName\":\"Watson\"}";
var jObj = JObject.Parse(jsonString);
// Need to add a ROOT Key for this jObj...
Simillar to the existing question How to add a key to a JSON array value?
You're not adding a property to the object, you're putting the object in another object. Just think of it as a dictionary. Create a "dictionary" and add your object to it. Then you could get the json string back from that object.
var jsonString = "{\"FirstName\":\"Emma\",\"LastName\":\"Watson\"}";
var jsonObj = JObject.Parse(jsonString);
var newObj = new JObject
{
["PersonName"] = jsonObj,
};
var newJsonString = newObj.ToString();
My question is simple but i can not do that. i wanna get value of "soap:Body" from below string by C#code?
{"soap:Envelope":{"xmlns:xsd":"http://www.w3.org/2001/XMLSchema","xmlns:soap":"http://www.w3.org/2003/05/soap-envelope","xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance","soap:Body":{"ToplamaResponse":{"xmlns":"http://tempuri.org/","ToplamaResult":156758}}}}
You can also use the Framework class JavaScriptSerializer if you do not want to use an external library.
string json = #"...";
JavaScriptSerializer serializer = new JavaScriptSerializer();
var o = serializer.Deserialize<dynamic>(json);
var body = o["soap:Envelope"]["soap:Body"];
You can do it easily by using Json.NET
dynamic data = JObject.Parse("{'soap:Envelope':{'xmlns:xsd':'http://www.w3.org/2001/XMLSchema','xmlns:soap':'http://www.w3.org/2003/05/soap-envelope','xmlns:xsi':'http://www.w3.org/2001/XMLSchema-instance','soap:Body':{'ToplamaResponse':{'xmlns':'http://tempuri.org/','ToplamaResult':156758}}}}");
string soap_body = data["soap:Envelope"]["soap:Body"];
There is a simple example in the JObject.Parse documentation
string json = #"{
"soap:Envelope": {
"xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
"xmlns:soap": "http://www.w3.org/2003/05/soap-envelope",
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"soap:Body": {
"ToplamaResponse": {
"xmlns": "http://tempuri.org/",
"ToplamaResult": 156758
}
}
}
}";
JObject obj = JObject.Parse(json);
Console.WriteLine((string)obj["soap:Envelope"]["soap:Body"]);
And if you want to manipulate the value of "soap:Body" do the same thing :)
I'm new to C# so I don't know how JSON Parsing in C# much. So I'm using JSON.net at the moment, so this is what I got:
WebClient c = new WebClient();
var data = c.DownloadString("http://media1.clubpenguin.com/play/en/web_service/game_configs/furniture_items.json");
JObject o = JObject.Parse(data);
button3.Text = "" + o["furniture_item_id"];
Is there any other way to parse JSON files with the JSON URL?
try
WebClient client = new WebClient();
string getString = client.DownloadString("http://media1.clubpenguin.com/play/en/web_service/game_configs/furniture_items.json");
JavaScriptSerializer serializer = new JavaScriptSerializer();
var listOfFurniture = serializer.Deserialize<List<Furniture>>(getString);
public class Furniture
{
public int furniture_item_id { get; set; }
}
There are many furniture_item_id's returned by the webclient. I've created a simple object that holds that value. 'listOfFurniture' is a List of type Furniture.
dynamic json = JsonConvert.DeserializeObject(data);
You can then access json as array by using an index or a key:
var someval = json["furniture_item_id"];
Note:
In case the request was not successful and thus you do not have any response to parse, this will probably end up in an JsonParseException so you might want to catch that.
How to deserialize with keyvaluepair the above json
string stations = [{"2":false,"1":"Aforre","0":"WS6"},{"2":false,"1":"Alodtau","0":"WS3"},{"2":false,"1":"Balimo Station","0":"WS36"}]
I what like this
var get = js.Deserialize<Dictionary<string,dynamic>>(stations);
try this:
string stations = "[{'2':false,'1':'Aforre','0':'WS6'},{'2':false,'1':'Alodtau','0':'WS3'},{'2':false,'1':'Balimo Station','0':'WS36'}]";
var serializer = new JavaScriptSerializer();
dynamic value = serializer.DeserializeObject(stations);
and you can access objects like:
var a = value[0]["0"];
and a will have "WS6" (according to your JSON)
The JSON shown is an array. You might try desetializing to:
Dictionary<string, object>[]
i.e.
var get = js.Deserialize<Dictionary<string,object>[]>(stations);
One of the best option is create a class that consist of all keyvalue pair and then deserialize json string to the object of created class
You can try using dynamic variable as following:
string stations = "[{'2':false,'1':'Aforre','0':'WS6'},{'2':false,'1':'Alodtau','0':'WS3'},{'2':false,'1':'Balimo Station','0':'WS36'}]";
var serializer = new JavaScriptSerializer();
dynamic serializevalues = serializer.DeserializeObject(stations);
var valueof1 = serializevalues[0]["1"];
Response.Write(valueof1);
The above will print output "Aforre'.