Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract data from Json string

Tags:

I got a string containing Json. It looks like this:

"status_code":200, "status_txt":"OK", "data": {    "img_name":"D9Y3z.png",    "img_url":"http:\/\/s1.uploads.im\/D9Y3z.png",    "img_view":"http:\/\/uploads.im\/D9Y3z.png",    "img_width":"167",    "img_height":"288",    "img_attr":"width=\"167\" height=\"288\"",    "img_size":"36.1 KB",    "img_bytes":36981,    "thumb_url":"http:\/\/s1.uploads.im\/t\/D9Y3z.png",    "thumb_width":360,    "thumb_height":360,    "source":"http:\/\/www.google.com\/images\/srpr\/nav_logo66.png",    "resized":"0",    "delete_key":"df149b075ab68c38" } 

I am trying to get a hold of the "img_url". I have Json.NET installed and I´ve found similar questions here..

for example something like this:

JObject o = JObject.Parse("{'People':[{'Name':'Jeff'},{'Name':'Joe'}]}");  // get name token of first person and convert to a string string name = (string)o.SelectToken("People[0].Name"); 

In my case I changed ("People[0].Name") to ("img_url"),("img_url[0]) etc..no luck

This is my code now:

public string tempJson { get; set; } public ActionResult SaveUploadedFile(string test) {     using (WebResponse wrs = wrq.GetResponse())     using (Stream stream = wrs.GetResponseStream())     using (StreamReader reader = new StreamReader(stream))     {         string json = reader.ReadToEnd();         tempJson = json;     } } 

Do I have to do something with the string before I can extract the value? Thanks!

like image 288
user2915962 Avatar asked Jun 15 '14 19:06

user2915962


People also ask

How do I parse a string in JSON?

Example - Parsing JSONUse the JavaScript function JSON.parse() to convert text into a JavaScript object: const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}'); Make sure the text is in JSON format, or else you will get a syntax error.

Can I convert JSON to text?

Users can also Convert JSON File to Text by uploading the file. Download converted file with txt extension. JSON to Text Online works well on Windows, MAC, Linux, Chrome, Firefox, Edge, and Safari.

How do I query JSON data?

To query JSON data, you can use standard T-SQL. If you must create a query or report on JSON data, you can easily convert JSON data to rows and columns by calling the OPENJSON rowset function. For more information, see Convert JSON Data to Rows and Columns with OPENJSON (SQL Server).

How extract particular data from JSON in Python?

Converting JSON file to Python object Apart from JSON, Python's native open() function will also be required. Instead of the JSON loads method, which reads JSON strings, the method used to read JSON data in files is load(). The load() method takes up a file object and returns the JSON data parsed into a Python object.


1 Answers

img_url is not a property of root object - it's a property of data object:

var obj = JObject.Parse(json); var url = (string)obj["data"]["img_url"]; // http://s1.uploads.im/D9Y3z.png 

Another option:

var url = (string)obj.SelectToken("data.img_url"); 
like image 67
Sergey Berezovskiy Avatar answered Sep 19 '22 19:09

Sergey Berezovskiy