I am working on a Scala app. I have a method that gives me JSON and I convert it to string using toString as follows:
def myjson(fileName:String){
val myJson = myData.getJsonData().toString
}
Here getJsonData() will give me a .json. I want to write this .json into a file and save this .json in resources section in my project. Format of the file should be ".json". Name of the file is the one which I am getting in above method. How can I do that?
One approach would be as follow
import java.nio.file.{Paths, Files}
import java.nio.charset.StandardCharsets
def myjson(fileName:String){
val myJson = myData.getJsonData().toString
val path = s"/user/myProject/..../resources/$fileName"
Files.write(Paths.get(path), myJson.getBytes(StandardCharsets.UTF_8))
}
I understand that fileName: String will be something like filename.json
Related
I am unable to parse json file if values contain single quote and other characters as '()= etc
Hi I have the following Json file:
{
"config": {
"inputDFName": "testInput",
"conditions": "site_type = 'micro'"
}
}
The parser class like
case class FilterConfig(inputDFName: String,conditions: String)
I am parsing json using below function
def readDataFilter(conf:String): FilterConfig ={
val gson = new Gson()
gson.fromJson(conf,classOf[FilterConfig])
}
But return error like : com.google.gson.JsonSyntaxException:com.google.gson.stream.MalformedJsonException
but if i remove single quotes and equal to sign its working fine.
Please help here.
I see two problems here.
inputDFName in JSON is not matching with testDFName in FilterConfig class.
Your JSON structure is different from that of FilterConfig.
I tried to run the code with the above changes and its working fine.
case class FilterConfig(inputDFName: String,conditions: String)
import com.google.gson.Gson;
def readDataFiltr(conf:String): FilterConfig ={
val gson = new Gson()
gson.fromJson(conf,classOf[FilterConfig])
}
val x = readDataFiltr("{\"inputDFName\": \"testInput\", \"conditions\": \"site_type = 'micro'\"}")
println(x.inputDFName)
println(x.conditions)
Please let me know if this is not clear.
Thanks,
Vivek
I have a JSON file in the flutter directory, not in assets.
json_data.json:-
{
"foo" : "bar"
}
I want to read this JSON on different files.
like
myfile.dart:-
import "package:mypackage/json_data.json" as data;
import 'dart:convert';
var my_data = json.decode(data);
I am getting the error:-
The name 'data' refers to an import prefix, so it must be followed by '.'.
Try correcting the name to refer to something other than a prefix, or renaming the prefix.
What is the problem here? why can't I read JSON from local import in flutter?
You should look into loading assets in flutter. You can't simply import an arbitrary file. Importing is for source code/libraries.
You need to declare this file as an asset in your pubspec.yaml
flutter:
assets:
- json_data.json
Then in your code you can load this asset as a String:
import 'package:flutter/services.dart' show rootBundle;
Future<String> getJson() {
return rootBundle.loadString('json_data.json');
}
You can decode the JSON with your existing code, but it should be placed in a method body somewhere. You call this getJson function to retrieve the JSON String:
var my_data = json.decode(await getJson());
Alternatively, you could simplify this even further by putting the contents of your JSON file directly into the code as a String, but this may not be possible, it depends on your intended use of this JSON.
const String data = '''
{
"foo" : "bar"
}
''';
var my_data = json.decode(data);
Suppose Some JSON file is # www.github.com/xyz/Hello.json, I want to read this content of this JSON in JSON object in Groovy
You then need:
import groovy.json.JsonSlurper
def slurped = new JsonSlurper().parse('www.github.com/xyz/Hello.json'.toURL())
Here you can find more info.
I am trying to: 1. read a json file with groovy; 2. change the object a little bit; 3. output/override as a new json file.
so far, I know we can use import groovy.json.JsonSlurper;, but I searched some samples, all for parse the .txt file (convert txt to json format object) like this:
def inputFile = file('json_input/' + fileName)
def inputJson = new JsonSlurper().parseText(inputFile.text)
print out...
but not read json file directly.
Is there any simple way to read and get the json object from a .json file so that I can make some changes of the data?
Thanks!
I'm trying to load a json file from a URL and parse it within Dart. So I had tried the following code as suggested from some links when I google for it:
HttpRequest.getString("hellknight2.js").then((response)
{
var model = new JSON.parse(response);
});
However, it seems to not work anymore on Dart SDK version 0.4.3.5_r20602. What is the current best way to get a Json file mapped to an object in Dart?
Simply use json of the dart:convert package. Here is an example :
import 'dart:convert';
main() {
final myJsonAsString = '{"a": 1, "b": "c"}';
final decoded = json.decode(myJsonAsString);
....
}
See Parsing JSON for more details.
in my case
JSON.decode
didn't work.
Instead I had to use :
import 'dart:convert' as JSON;
final json=JSON.jsonDecode(myJsonAsString);
Here is my solution :) At first, you need to import the convert package:
import 'dart:convert';
var res = json.decode(response.body);
then you can get values by key, like below:
print(res["message"]);
It depends on a lot of things.
Is the json text you get is an array or a map?
You can try with:
Map model = new parse(response);
Or
List model = new parse(response);
but you need to import JSONObject by Chris Buckett into your package
import "package:json_object/json_object.dart";
You can install it from pubspec adding this dependency
json_object
There's a new pub package for this:
Victor Savkin - Serializers.
I didn't use it but seems to me that it will suite you. Try it out
you can try this package. pub: g_json
dynamic model = JSON.parse(JsonStringFromAnywhere);
final name = model['name'].stringValue;
// OR
final name = model.name;