How to parse json string in ajax? [closed] - ajax

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have response like this how to parse this data ? any suggestions
{"response":[{"job_id":"29","message":"egfn","lattitude":"26.9514731","longitude":"89.4524783","job_status":"P","date":"2013-11-21
03:33:34","username":"users"},{"job_id":"28","message":"egfn","lattitude":"26.9514731","longitude":"89.4524783","job_status":"P","date":"2013-11-21
03:33:34","username":"rajeev"},{"job_id":"27","message":"egfn","lattitude":"26.9514731","longitude":"89.4524783","job_status":"P","date":"2013-11-21
03:33:34","username":"pankaj"},{"job_id":"26","message":"test job for
you","lattitude":"25.4558641","longitude":"80.1325838","job_status":"P","date":"2013-11-21
01:09:58","username":"users"},{"job_id":"24","message":"test job for
you","lattitude":"25.4558641","longitude":"80.1325838","job_status":"D","date":"2013-11-21
01:09:58","username":"amit"},{"job_id":"25","message":"test job for
you","lattitude":"25.4558641","longitude":"80.1325838","job_status":"P","date":"2013-11-21
01:09:58","username":"test"}]}
Thanks in advance

You can do this:
JSON.parse('{"response":[{"job_id":"29","message":"egfn","lattitude":"26.9514731","longitude":"89.4524783","job_status":"P","date":"2013-11-21 03:33:34","username":"users"},{"job_id":"28","message":"egfn","lattitude":"26.9514731","longitude":"89.4524783","job_status":"P","date":"2013-11-21 03:33:34","username":"rajeev"},{"job_id":"27","message":"egfn","lattitude":"26.9514731","longitude":"89.4524783","job_status":"P","date":"2013-11-21 03:33:34","username":"pankaj"},{"job_id":"26","message":"test job for you","lattitude":"25.4558641","longitude":"80.1325838","job_status":"P","date":"2013-11-21 01:09:58","username":"users"},{"job_id":"24","message":"test job for you","lattitude":"25.4558641","longitude":"80.1325838","job_status":"D","date":"2013-11-21 01:09:58","username":"amit"},{"job_id":"25","message":"test job for you","lattitude":"25.4558641","longitude":"80.1325838","job_status":"P","date":"2013-11-21 01:09:58","username":"test"}]}')
EDIT:
to get all job_id do this:
var obj = JSON.parse('{"response":[{"job_id":"29","message":"egfn","lattitude":"26.9514731","longitude":"89.4524783","job_status":"P","date":"2013-11-21 03:33:34","username":"users"},{"job_id":"28","message":"egfn","lattitude":"26.9514731","longitude":"89.4524783","job_status":"P","date":"2013-11-21 03:33:34","username":"rajeev"},{"job_id":"27","message":"egfn","lattitude":"26.9514731","longitude":"89.4524783","job_status":"P","date":"2013-11-21 03:33:34","username":"pankaj"},{"job_id":"26","message":"test job for you","lattitude":"25.4558641","longitude":"80.1325838","job_status":"P","date":"2013-11-21 01:09:58","username":"users"},{"job_id":"24","message":"test job for you","lattitude":"25.4558641","longitude":"80.1325838","job_status":"D","date":"2013-11-21 01:09:58","username":"amit"},{"job_id":"25","message":"test job for you","lattitude":"25.4558641","longitude":"80.1325838","job_status":"P","date":"2013-11-21 01:09:58","username":"test"}]}')
var job_id_array = new Array();
for(var i = 0;i< obj.response.length ; i++){
job_id_array.push(obj.response[i].job_id);
}

You can use JQuery.parseJSON()
More details : http://api.jquery.com/jQuery.parseJSON/

If your JSON string would be stored in the json variable, you may do the follwoing (given that the environment supports ECMAScript5):
var o = JSON.parse(json);
var result=[];
o.response.forEach(function(e){
result.push(e.job_id);
});
console.log(result);
which outputs the content of the result array:
["29", "28", "27", "26", "24", "25"]
Here's a live demo and a good answer which addresses your question.

Related

Array to string conversion when run eloquent command [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Controller :
public function index(){
$data = Personal::all();
return view('frontend/dashboard');
}
Error :
Array to string conversion
And also show :
\vendor\laravel\framework\src\Illuminate\Database\Grammar.php:39
change
return view('frontend/dashboard');
to
return view('frontend.dashboard');
please make sure your file name is dashboard.blade.php

how to check wether a form input has file laravel? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I want to check whether form has file or not of it has I want to upload it to server and save path if not then ignore and run old dB saved path query
You can try in this way first validate your all inputs as per your requirement then check for the file in this way :
Am assuming logo file here from form filed :
if($request->hasFile('logo')){
// Get filename with the extension
$filenameWithExt = $request->file('logo')->getClientOriginalName();
// Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
// Get just ext
$extension = $request->file('logo')->getClientOriginalExtension();
$fileNameToStore=$filename.'_'.time().'.'.$extension;
// Upload Image
$path = $request->file('logo')->move('webimg/', $fileNameToStore);
// Filename to store
$fileNameToStore= $path;
use the hasFile method on the request:
if ($request->hasFile('name_of_file_input_fiels')) {
//
}
https://laravel.com/docs/5.6/requests#retrieving-uploaded-files
if ($request->file('name_of_file_input_fields')) {
}
you can check also this way.
use laravel File validator
$rules = array(
'file' => 'required'
);
You can check documentation here
Here is an example: Tutorial with file upload validation
It's pretty simple.
$request->hasfile('your key');

Laravel - How to use isDirty() [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying to use Laravel isDirty() which helps me to get only modified values,
But there is a problem when I'm trying to use, it always return false
public function update(Request $request, $id) {
$client = Client::find($id);
dd($client->isDirty('name'));
}
It always return false.
It returns false because you haven't done anything to $client.
Thus, it is not "dirty" - it is "clean".
If you do something like $client->name = str_random(40); it'll become dirty.
To get only modified attributes, you need to use the getDirty() method. isDirty() only shows if there are any modified attributes:
$client = Client::find($id);
$client->name = 'Some New Name';
$modifiedAttributes = $client->getDirty();
If you want to check if any of properties were modified in the submitted form or not, you can do it like this:
if ($client->name === $request->name)

Unable to properly create nested hash after reading data from Excel [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
There is one quiz excel sheet which has 3000 questions.which has question_no, options, test_name, category, question_text,correct_answer,tags fields. using "Spreadsheet" gem i have parsed data from excel.
Now I have one hash which stores "tags type" as key and tag related questions are stored in an array as value.
if questions_hash[tags_string] == nil
questions_hash[tags_string] = []
questions_hash[tags_string].push question
which look like this,
tags = {"tag1" => [value],"tag2" => [value] }
and other hash is,
categories = {"cat1" => "", "cat2" => "" }
and i want to assign tags hash as a value in categories hash.like
categories = {"cat1" => {"tag1" => [value], "tag2" => [value]} ,"cat2" => {"tag3" =>[value] }}
how can i do this?
Hash does has push method.
To insert value into Hash use simple assignment as:
questions_hash[tags_string] = question
Similarly, if you want to insert questions_hash as value to catetory hash use:
hash['History'] = questions_hash
That's it.

Linq update list whilst looping through list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a list which is accessible as follows:
List<PingtreeNode> _pingtree;
IEnumerable<PingtreeNode> ActiveNodes
{
get { return _pingtree.Where(x => x.Status == PingtreeNodeStatus.Ready);}
}
I also have a method, who's job it is to iterate over some of the ActiveNodes where a RuleClass exists. For each of these nodes, a validation method is called and if the node is found to be invalid, a new PingtreeNodeStatus is set.
void ValidateBuyerRules()
{
var nodes = ActiveNodes.Where(x => x.RuleClass.IsNotNullOrEmpty());
foreach (var n in nodes)
{
var isValid = NewRuleSet(n.RuleClass).Validate(_appForm);
if (!isValid) n.Status = PingtreeNodeStatus.FailedCritera;
}
}
One of the properties of PingtreeNode is 'Product', and a product can exists several times within the list ActiveNodes.
What I want to do is this. If isValid is true for a product, I need to set all subsequent nodes containing the same Product to a PingtreeNodeStatus.FailedCriteria. In other words ignore them further down the nodes list and don't perform validation on them.
I know how to iterate through the loop and do this but am concerned about updating a list whilst already being inside the list. Is there a safe way of achieving this?
It's not possible to update a list while iterating over it, however that's not what you're doing.
You're not modifying the list but properties of the items in the list, this is perfectly safe.

Resources