Actually I am receiving a JSONArray and I loop over every JSONObject. However, some json fields contain special characters. Forxample: ä, ö, Ä and etc. A sample jsonobject is shown below:
{
"field1": null,
"field2": "Äpple",
"field3": 1,
"field4": null
}
field2 is being displayed as Ã\u201epple.
Is this some encoding issue? How can I fix this issue?
I am using the following piece of code to read the JSON objects in the array.
Object dataObject = res.getJSONObject(i).get(column);
tempObject.put(column, dataObject);
Now the tempObject contains the column 'field2' with the distorted value.
Have you tried to set response encoding to utf-8?
res.setContentType("application/json;charset=UTF-8");
If sample is given as array then to read that array , you can convert JSON array into JsonObject and then fetch field1,field2 values as string.
e.g
jarr.get(i).getAsJsonObject().get("field1").getAsString();
Related
I saved a json into the database as a string like this:
"[_district:_1_2_5village, _name:_1_1_2id_inter, _gender:_1_3_5sex]"
Now i want to convert it back to a Json Object so as to pick the key and value eg _district is the key and _1_2_5village is the value. Any help on how i can achieve this. Thanks
I tried to convert the string back into JSON by parsing but that dint work for me.
It doesn't work because that's not a JSON format, a JSON is a way of mapping objects and uses key value syntax like so:
{"key": "value"}
and an array would look like this:
[{"key": "value"},{"key": "value"}]
You'll need to make a custom parser for your syntax
Here's the json specification:
https://www.json.org/json-en.html
I have JSON as string
"{nameBitsCount=131}"
I need, using Jackson: 1) Parse this JSON correctly. 2) Put result into Map<String, Long>
But I Getting exception.
com.fasterxml.jackson.core.JsonParseException: Unexpected character ('n' (code 110)): was expecting double-quote to start field name
at [Source: (String)"{nameBitsCount=131}";
PS: I think I need to change style of JSON to this
"{\"nameBitsCount\":\"131\"}";
The JSON you send isn't valid
{nameBitsCount=131}
need to convert format :
{"nameBitsCount":131}
add {"} around the Key and change {=} to {:} the JSON format valid for Json Object :
{
"Key" : "Value"
// "VALUE" if use for String and char put {"} around the value VALUE ex: 0.0 , 1 , -50 , FALSE
}
Look this website :
JSON Website
JSON Syntax
I am developing an app that communicates with Database, to retrieve values. I am using PHP for the Backend, and developing on Android Studio, using the Volley Library.
My problem is, the values that I need to send are multiple records of a table, each with four columns, for example name, age, department, and country. I am using JSON to encode these values, but I need help with how to proceed. Should I use JSON encoded 2D Arrays? if so, how to make use PHP to construct this array, as there can be variable numbers of rows.
Also, How to parse that JSON Object/Array in Android (Java)?
As of now, this is my progress:
JSON Output in browser:
{"name0":"ABC","age0":"25","department0":"Medical","country0":"XYZ","name1":"DEF","age1":"26","department1":"Engg.","country1":"XYZ"}
Here, I named each "key" of JSON using a Loop in PHP, and encoded as JSON Object. But Having Difficulty in displaying this in Android. I have used a XML layout with 4 textviews, and LISTVIEW in the main Activity XML File.
I would suggest a different json structure for encoding. Yours will get messy pretty quick if there are a lot of records. For example you would have name0, name1, ... nameN. It would be better to make an array like so:
[
{
"name" : "ABC",
"age" : 25,
"department" : "Medical",
"country" : "XYZ"
},
{
...
}
]
Notice that there are no indices concatenated to your keys. You can get the index based on the json object node's position in the array if you need it.
As for parsing it in Android, you can refer to the documentation. There is a Json parser that comes with the SDK so all you need to do is read in your string as a json array and iterate over its object nodes as needed.
For example
String jsonResponse = " ... "; // whatever the php backend gives you when you make a call to the endpoint
JSONArray arr = new JSONArray(jsonResponse);
for (int i=0; i<arr.length(); i++) {
JSONObject obj = arr.get(i);
String name = obj.getString("name");
...
}
I've got a JSON string persisted in a DB (in a column of type Text), which I would like for clients of the server to be able to retrieve through a REST API.
Problem is that once the JSON string is retrieved by the backend and stored in a String object, ready to be returned to the client as part of the response body, Java automatically escapes the quotes.
So a JSON string persisted as:
{"key1": "value1", "key2": "value2"}
Would be retrieved and stored in a string object as:
{\"key1\": \"value1\", \"key2\": \"value2\"}
Then this escaped string would be returned to the client. My worry is that in the format that it is it won't be usable by the client.
I've seen some people using regex to remove the backslash, but I'm not sure how feasible that would be as a solution.
Any suggestions?
You can use org.apache.commons.text.StringEscapeUtils class for doing this. Please refer below code
String s = "{\\\"key1\\\": \\\"value1\\\", \\\"key2\\\": \\\"value2\\\"}";
s = StringEscapeUtils.unescapeJava(s);
System.out.println(s);
gives
{"key1": "value1", "key2": "value2"}
I need to be able to parse sql calls from a database to json and then compare the key fields in the parsed JSON string against a json file (it's technically a BOD --Business Object Document) and if they match then I need to overwrite the json file's matching value with that of the JSON string.
e.g. I parse the sql call to this
{
"partyInfo": {
"PARTY_NAME": "NORWAY",
"STATE": "OSLO",
"PARTY_ID": "92706031",
"VERTICAL_MARKET_TOP_DESC": null,
"ATTRIBUTE20": null,
"DUNS_NUMBER": null,
"SIC_CODE": null,
"EMPLOYEES_TOTAL": null,
"ALL_ADDRESS_LINES": "HOMMENKOLLEN 23 TOPPEN 12",
"CITY": "OSLO",
"POSTAL_CODE": "1255",
"COUNTRY_NAME": "NORWAY",
"KNOWN_AS": null
}
}
and then compare it against a file that looks like this:
{
"partyInfo": {
"PARTY_NAME": string,
"STATE": string,
"PARTY_ID": number,
"SIC_CODE": string,
}
}
and overwriting values on matching keys s.t that the ending file looks like this:
{
"partyInfo": {
"PARTY_NAME": "NORWAY",
"STATE": "OSLO",
"PARTY_ID": "92706031",
"SIC_CODE": null,
}
}
So far I've been able to parse the SQL calls to JSON (using Jackson right now but I'm willing to change if need be) but I don't know how to compare against the file and overwrite only the data values that match up.
It looks like the tree model allows you to update nodes.
Here's an example:
http://wiki.fasterxml.com/JacksonInFiveMinutes#Tree_Model_Example
I may be misunderstanding what you're wanting to do, but it sounds like you could just use a for loop over the keys in the Map representing the JSON from the file, compare the values to those in the database, change any that don't match, and then write the file back out:
for(String key: fileJson.keys())
if(!fileJson.get(key).equals(sqlJson.get(key)))
fileJson.put(key, sqlJson.get(key));
// write fileJson back out to the correct file through Jackson
The simplest solution would be to parse these files into java objects (using Jackson again), compare the objects and then save what you need to save.
Otherwise you'll effectively be making something like the patch tool.