I write a simple program for fetching some json data and I hit by this error
Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $
I searched in stackoverflow and I didn't get the answer
but I figured out this error is because of "JSON_PRETTY_PRINT" code in json encode or the pre tag maybe
.
I changed this :
echo "<pre>".json_encode($row,JSON_PRETTY_PRINT)."</pre>";
to this :
echo json_encode($row);
and it worked. But the printed json is really messy and ugly. Do you have any solution for this problem ?
Use retrofit library and probably you are sending an array but on parsing you parse it as an Object not an array, thats problem
Related
I'm making an app that retrieves a JSON file from a server to locate a marker on a map. My problem is that the JSON file that I'm retrieving from the server has multiple root elements.
This is an example of what the JSON file looks like.
{"class":"VERSION","release":"3.11","rev":"3.11-3","proto_major":3,"proto_minor":9}
{"class":"DEVICES","devices":[{"class":"DEVICE","path":"/dev/ttyUSB0","driver":"NMEA0183","activated":"2017-03-25T20:20:20.649Z","flags":1,"native":0,"bps":9600,"parity":"N","stopbits":1,"cycle":1.00}]}
{"class":"WATCH","enable":true,"json":true,"nmea":false,"raw":0,"scaled":false,"timing":false,"split24":false,"pps":false}
{"class":"TPV","tag":"RMC","device":"/dev/ttyUSB0","mode":3,"time":"2017-03-25T20:20:21.000Z","ept":0.005,"lat":18.487301667,"lon":-69.942863333,"alt":73.300,"epx":3.788,"epy":3.496,"epv":6.612,"track":352.7300,"speed":0.010,"climb":-0.100,"eps":7.58,"epc":13.22}
Since I don't handle the server nor what pases the JSON file with the info to the server. Is there any way to process this file as it is? and how? I know it's not in a valid JSON format. Is there a way to only read the TPV class in the app on android?and how? & Is it possible to do any of this still on the AsyncTask?
You can add an [ to beginning of the string and a ] to end and treat it as JsonArray.
With the json response I am not getting the jsonschema2pojo class.
I am getting error like this "There's a problem: Unexpected character ('r' (code 114)): was expecting double-quote to start field name (line 2, column 2)".
I am newer in retrofit.Please help me.Thanks in advance.
Finally I got the solution that you need to put "" to all of your response parameter.
I'm currently deveolping an Android application that has Django framework as it's server side.
When i'm posting a data of a new user to my database i am POSTing a multipart request that has a user part inside.
The user for some reason is represented as a list but when i take it out of the request.data['user'] it's a str instance (Yea i dont know why...)
When i fetch that str i started working on it with json package.
I looked up on the internet (to many places..) how to convert a string in json format to a dictionary.
What i found is that when you use the json.loads command it doesn't give a dict back but a str instance :)
This is the code on my server side when i enter the create function of the ModelViewSet that handles the creation of the user.
userJson = request.data['user']
userJson = json.dumps(userJson)
userJson = json.loads(userJson)
What i tried to do is to make a string of my own in JSON format and that called the json.loads() command which gave me the dict object..
There seems to be a problem with processing the str from the http request of django rest framework for some reason or there's something else i am not seeing.
I tried the following links -
Converting JSON String to Dictionary Not List
http://docs.python-guide.org/en/latest/scenarios/json/
Didn't worked also..
Now, i tried accessing the str i got from json.loads() like a dictionary in this way.
id = userJson['id']
Now lets say maybe i passed a wrong json format to the loads function, it should have thrown an exception..
The code above (getting the id) raised an exception of 'String indices must be integer' - it doesn't convert it to dict! LOL xD
Good note worth mentioning - I'm trying to convert the json to a dictionary so i could access it like this - dictObject['id']
Well i would really appreciate every help!
Thanks :)
For some reason , when i did this commands-
userJson = request.data['user']
userJson = json.loads(userJson)
userJson = json.loads(userJson)
What i got to have inside the userJson after the second json.loads(userJson) I got the actual dict object to the userJson member.
Appearently it is a bug.
21 January - another update, I truly was doing double Json encoding on the Android application so that was the reason for double json. loads()
i have a running app but i had a little problem with my sever,i have yet to solve my server problems and i'm encountering this problem with my log in the error comes from the authentication method.the error says jsonarray cannot be converted to jsonobject i've made a pastbin of my log and i was hoping someone can take a look at it and tell what my problem is.i think my problem is that i cant access my database but im not really sure if its the problem or my problem is my json.i cant hink of way to authenticate my username and password withour having problem like "at org.json.JSON.typeMismatch(JSON.java:111)" here is my paste bin http://pastebin.com/zgzbxcSV
you get from the server a (maybe empty) json array and you are try to convert it in a JsonObject.
instead of
JSONObject obj = new JSONObject(string);
you should do
JSONArray obj = new JSONArray(string);
You are trying to convert JsonObject to JsonArray so getting error.
You have to convert proper jsonObject to another object as per your requirement.
You can find useful demo with explanation.
If you find and trouble for parsing your json after researching this demo then let me know.
I'm trying to follow this tutorial in order to connect to an external database with android:
http://www.helloandroid.com/tutorials/connecting-mysql-database
However, I seem to receive two different errors when viewing the php / mysql connection code directly
and through logcat.
If I look at it through a browser, it comes up with the orange php error saying:
Undefined index: year
Found on line 6 with .$_REQUEST['year'].
If i debug the app and look at the logcat output, it seems to give a half error:
E/log_tag ( 403): Error parsing data org.json.JSONException: A JSONArray text m
ust start with '[' at character 6 of
On the php page, below the error, Json prints off the tables correctly, and echoing mysql errors are fine.
Can anyone help with the implementation of this function $_REQUEST please? (If that is the problem.)
Your output comes from the http request must be a well formed json string.
Obviously a PHP error like an Undefined index can break the json string and makes it unparsable by java.
You need to rid off the notice, for istance:
<?php
if (isset($_REQUEST['year']) && $_REQUEST['year'] == 'foo'){
//Do query and output back the json array
}
?>
BTW $_REQUEST is not a function. Is a superglobal associative array where all request variables (GET/POST/COOKIE) are stored.