So there is 1 website from where I need to get this JSON data.
The link is direct (I am using the website's API )
THe problem is, the file is HUGE!. Tens of Thousands of Lines.. Even more..
I have visual studio 2013 and what I need to do is download that JSON data in a callback and then parse it to get a specific value. I am using Newtonsoft.JSON to parse it and here is what I have thought will be able to parse it
var obj = JsonConvert.DeserializeObject<JContainer>(jsonText);
var value = (int)obj["response"]["prices"]["5021"]["6"]["0"]["current"]["value"];
The problem is, how do I download all of that data and convert it into C# classes? Is there another way?
Thanks a lot.
EDIT: If not JSON, I have option to download it in JSONP and VDF format
Here is the link of the JSON data - http://backpack.tf/api/IGetPrices/v3/?format=json&key=52f75dab4dd7b82f698b4568
I got it working by doing this
using (var webClient = new System.Net.WebClient())
{
var json = webClient.DownloadString("http://backpack.tf/api/IGetPrices/v3/?format=json&key=00a00aaa0aa0a00a000a0000");
Newtonsoft.Json.Linq.JObject o = Newtonsoft.Json.Linq.JObject.Parse(json);
var value = (int)o["response"]["prices"]["5021"]["6"]["0"]["current"]["value"];
Console.WriteLine(value);
}
Thanks for your help everybody!!
Try restsharp. It let you do something like
var prices = client.Execute<Prices>(request);
Where Prices is the class that matches the returned schema
Looking at the comment Brandon posted, he's right in principle, but you don't have to switch to Newtonsoft if you don't want. You just need to use a different JSON.NET API
var serializer = new JsonSerializer();
using (var stream = File.OpenRead("C:\\Users\\gweakliem\\Downloads\\sotest.js"))
{
using (StreamReader streamReader = new StreamReader(stream))
{
using (JsonReader reader = new JsonTextReader(streamReader))
{
var aThing = serializer.Deserialize<JContainer>(reader);
var aValue = (int) aThing["response"]["prices"]["5021"]["6"]["0"]["current"]["value"];
Console.WriteLine("Read a value " + aValue);
}
}
}
If you're concerned about blocking on this thread while reading, it looks like you're going to have to write some code. I don't see awaitable methods on JsonTextReader or JsonSerializer, so I expect that those methods will block.
Now if you want to turn this into objects, here's a couple other SO posts:
deserialize json into .net object using json.net
dynamic JContainer (JSON.NET) & Iterate over properties at runtime
Or this post covers a bunch of deserialization options.
Related
I have to consume a so called web service implemented by a dumb monkey which is returning some garbage after the proper Json response.
Something like this:
{
"Property1": 1,
"Property2": 2,
"Property3": 3
}<?xml version='1.0' ?>Maybe some other gibberish nonsense I wish to discard.
Now, I could just search for "<?xml" and split, but I was wondering if I can use a stream reader or something to read up to the closing } and then discard the rest.
I'm using C# and Json.Net.
You can also set JsonSerializerSettings.CheckAdditionalContent = false to tell the serializer to ignore any content after the end of the deserialized JSON object:
var result = JsonConvert.DeserializeObject<Dictionary<string, long>>(json, new JsonSerializerSettings { CheckAdditionalContent = false })
Oddly enough it is necessary to do this explicitly despite the fact that the default value seems to be false already, since the underlying field is nullable.
I knew there had to be a simple and robust way:
public T ReadTypeAndDiscardTheRest<T>(string json)
{
using (var sr = new StringReader(json))
using (var jsonReader = new JsonTextReader(sr))
{
var token = JToken.Load(jsonReader);
return token.ToObject<T>();
}
}
[Test]
public void TestJsonDiscarding()
{
var json = #"{""Key"":""a"", ""Value"":""n""}<?xml>aaaa";
var kp = ReadTypeAndDiscardTheRest<KeyValuePair<string, string>>(json);
Assert.That(kp.Key, Is.EqualTo("a"));
Assert.That(kp.Value, Is.EqualTo("n"));
}
As always, Json.Net FTW.
In my windows phone app, I'm in a need of creating a JSON object dynamically. i.e. I will know the property names only during run time. Also the property values can contain multiple lines.
Previously when I had to include multiple lines in a JSON object without any problem I used the following.
MemoryStream ms = new MemoryStream();
DataContractJsonSerializer ser = new DataContractJsonSerializer(obj.GetType());
ser.WriteObject(ms, obj);
using (StreamReader sr = new StreamReader(ms))
{
ms.Position = 0;
input = sr.ReadToEnd();
}
return input;
This worked very fine. But in order to use I should've known the class before hand. Unfortunately it is not possible.
Can anyone help me with any work around ?
Thank you.
DataContractJsonSerializer does not support such things.
You should try, for instance, Json.net.
I'm using protobuf-csharp-port and I need the ability to record some protobuf messages for replay later. XML would be ideal for me, but I'm flexible as long as a human could go into the file, make changes, then replay the messages from the file.
Using this C# code:
MyRequest req =
MyRequest.CreateBuilder().SetStr("Lafayette").Build();
using (var stringWriter = new StringWriter())
{
var xmlWriterSettings = new XmlWriterSettings
{
ConformanceLevel = ConformanceLevel.Fragment
};
var xmlWriter = XmlWriter.Create(stringWriter, xmlWriterSettings);
ICodedOutputStream output = XmlFormatWriter.CreateInstance(xmlWriter);
req.WriteTo(output);
output.Flush();
string xml = stringWriter.ToString();
using (var streamWriter = new StreamWriter(#"Requests.txt"))
{
streamWriter.WriteLine(xml);
streamWriter.WriteLine(xml);
streamWriter.WriteLine(xml);
}
}
I produce the Requests.txt file containing:
<str>Lafayette</str>
<str>Lafayette</str>
<str>Lafayette</str>
However when I try to deserialize them back using:
var xmlReaderSettings = new XmlReaderSettings
{
ConformanceLevel = ConformanceLevel.Fragment
};
using (var xmlReader = XmlReader.Create(#"Requests.txt", xmlReaderSettings))
{
ICodedInputStream input = XmlFormatReader.CreateInstance(xmlReader);
MyRequest reqFromFile;
while(!input.IsAtEnd)
{
reqFromFile =
ReverseRequest.CreateBuilder().MergeFrom(input).Build();
}
}
Only one MyRequest gets deserialized and the other two are ignored. (After reqFromFile is built, input.IsAtEnd == true.)
So back to my question: is there some way to read multiple human-readable protobuf messages from a file?
So back to my question: is there some way to read multiple human-readable protobuf messages from a file?
Well you're currently creating a single text file with multiple root elements - it's not a valid XML file.
The simplest approach would probably be to create a single XML document with all those requests in, then load it (e.g. with LINQ to XML) and create an XmlReader positioned at each of the root element's children. Each of those XmlReader objects should be able to then deserialize to a single protobuf message.
You could look at the Protocol buffer Editor
Alternatively there is Google own Text format which is a bit like JSon. You can convert between the 2 using the protoc command (look at encode / decode options, you also need the proto definition)
I'm using HttpWebRequest method to "GET" data from a specific url that the returned data supposed to be in json format. My code are something like
WebRequest request = WebRequest.Create("https://xxx.xxxxxxxx.com/xxxxxxx");
request.Method = "GET";
request.ContentType = "application/json";
var response = (HttpWebResponse)request.GetResponse();
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
}
The responseText value as I observed are
[
{
"webinarKey":5303085652037254656,
"subject":"Test+Webinar+One",
"description":"Test+Webinar+One+Description",
"organizerKey":73563532324,
"times":[{"startTime":"2011-04-26T17:00:00Z","endTime":"2011-04-26T18:00:00Z"}]
},
{
"webinarKey":9068582024170238208,
"name":"Test+Webinar+Two",
"description":"Test Webinar Two Description",
"organizerKey":73563532324,
"times":[{"startTime":"2011-04-26T17:00:00Z","endTime":"2011-04-26T18:00:00Z"}]
}
]
As you can see it is in json format, but I don't know how to set it as a json object, so I can get it's field value as something like
string webinarKey=responseText[0].webinarKey;
Am I right?
JavaScriptSerializer ser = new JavaScriptSerializer();
MyClass package = null;
package = ser.Deserialize<MyClass>(item);
Where item is your response text, and MyClass is the .net class you are returning. then you can access the properties of your object.
You have to parse the response (that is a textstring with JSON) with a JSON parser/deserializer. For example: Json.net
http://msdn.microsoft.com/en-us/library/bb412179.aspx
WCF has a DataContractJSONDeserializer.
You would need to define your types as .net objects that had properties that "look" like the json data coming back. I don't get the impression that you are actually using WCF in your application but you may still be able to use the DataContractJSONDeserializer anyway. You just need to instruct it the type you want it to deserialze as and the type needs to be marked with a DataContract attribute.
Heres plenty more info
http://msdn.microsoft.com/en-us/library/bb412170.aspx
You can almost always get away with using the JavaScriptSerializer class. There will be numerous variations on this, and I can see suggestions in other answers already, though this might well suffice. Namely, you'll want to look into the Deserialize<T> method, with a signature of:
public T Deserialize<T>(
string input
)
One advantage, if this suits, is that it is a readily available class in System.Web.Extension and removes the requirement for 'third party components'.
One possibility for this is to use a JObject instance. YOu can pass it a string and then extract the values easily:
JObject jobj = JObject.Parse(resultString);
someValue = jobj[0]["webinarKey"];
I am a newbie in asp.net MVC. I want to create a plain c# client program that consumes json returned from a asp.net mvc progam. What is the best method for retrieving the json data from the asp.net MVC site? I currently use WebRequst, WebResponse and StreamReader to retrieve the data. Is this a good method, otherwise what is the best practice to get the data? Can I use something like below? Thanks a lot
WebRequest request = HttpWebRequest.Create(url);
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string urlText = reader.ReadToEnd();
//Then parse the urlText to json object
You don't parse the text to JSON object on server side because JSON is Javascript Object Notation and C# knows nothing about that. You parse the JSON string to a specific type. For example:
string json = {"Name":"John Smith","Age":34};
Can be deserialized to a C# class Person as so:
public class Person
{
public string Name {get;set;}
public int Age {get;set;}
}
JavascriptSerializer js= new JavascriptSerializer();
Person john=js.Desearialize<Person>(json);
You can use the JavaScriptSerializer class:
var js = new JavaScriptSerializer();
var person = js.Deserialize<Person>(urlText);
Person, of course, should be replaced by your own .NET type. Here's also an article that might help you.
Well, one way is:
var dictionary = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(urlText);
You can use different types than a dictionary, but whether you should depends on why you're actually doing this.