Android does not parse JSON out put - android

{
"German":[
"Hello",
"guten Morgen",
"gute Nacht"
],
"English":[
"Hello",
"good morning",
"good Night"
],
"French":[
"bonjour",
"bonne nuit",
"bonjour"
]
}
In Android ,I have to parse this above output. I am unable to do. can any one tell me ,does it wrong jSON output or it is correct ?
http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

Its problem with your URL - saqib_abbasi.0fees.net/Response.php
Try after removing the underscore from your host name.
See the following links
https://stackoverflow.com/a/11206362/1329126
http://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names

It's a valid json. So you didn't do something right.
Make a JSONObject from the whole string, then you can get the arrays with getJSONArray("German") for example.

Related

How to fix this JSON response? I do not control the API

I'm working on an Android application that presents information from a public REST API (using Retrofit). One call is to gather bonus text information about a certain object. The call looks like this:
/text/1
The response I get is this:
{"textObject":[
{
"id":1,
"text":"{"More info" : {"url": "http://someurl.com/"}}"
}
]}
The string prints to {"More info" : {"url": "http://someurl.com/"}}. I don't think that is what they intended. I guess they wanted to include some more tags but I don't know.
Another answer might look like this:
/text/2
{"textObject":[
{
"id":2,
"text":"{" Placement":"The object is located on this spot"}"
}
]}
And the string of course prints to {" Placement":"The object is located on this spot"}
/text/3
{"textObject":[
{
"id":3,
"text":"{"Institutions":"Math, Computer Science, Informatics", "imageUrl": "http://www.someimageurl.com/img.jpg"}"
}
]}
/text/4
{"textObject":[
]}
I have no idea how to fix this, is it even at all possible without contacting the ones controlling the API?

Parse CloudCode won't show my app

I'm beginning to use CloudCode but I can't find my application after authentication.
Once I enter my email account and pass I get the following:
Email:
Pass:
1: results
Select an App:
From this step nothing works (Ex. parse deploy) and I get the error msg "Unknown application _default"
I'm using the latest parse-windows.2.0.11.exe, PowerShell on Win8.1
Should CloudCode be activate on the project in order to use it? if so, how is this done?
===UPDATE===
It turns out the global.json was created wrong (bug?)
{
"applications": {
"results": {
"applicationId": {
"appName": "AppName1",
"applicationId": "zzz",
"masterKey": "zzz"
},
"masterKey": {
"appName": "AppName2",
"applicationId": "xxx",
"masterKey": "xxx"
}
}
}
}
I've manually fixed it using this thread link
Hope this will help others..
=== END UPDATE===
Please advise.
Thanks,
Liran
My experience is with Android over Linux, but from what I see the base is the same - in Linux you create a folder for your CloudCode that you reference for deployments, so when you deploy you do that from inside that folder, read carefully through This Parse Tutorial - seems pretty similar.

Android push notification error in Corona

Even by using sample project provided in Corona SDk , I get a notification with error 400. I guess my json data is correct. Following is the code for Json message.
local jsonMessage =
[[
"registration_ids": ["]] .. tostring(googleRegistrationId) .. [["],
"data":
{
"alert": "Hello World!",
"sound": "default"
}
}
]]
This is the message on my device.
Based on the 400 error code, the problem must be your JSON :
400
Only applies for JSON requests. Indicates that the request could not
be parsed as JSON, or it contained invalid fields (for instance,
passing a string where a number was expected). The exact failure
reason is described in the response and the problem should be
addressed before the request can be retried.
With all the square brackets and html tag, it's really hard to understand from your question how your JSON actually looks like.
Anyway, here's how it should look like :
{
"registration_ids": ["some reg id"],
"data":
{
"alert": "Hello World!",
"sound": "default"
}
}
I solved this error by changing the format of my json and checking the format on this link http://jsonlint.com/# . This was a great help and also I replace alert icon by custom icon by using this:
http://developer.android.com/guide/practices/ui_guidelines/icon_design_status_bar.html

Reading Facebook Response With Jannson JSON Reader

So long story short I have a android app and I used cocos2dx to dev it. One component I am working on is bringing my facebook friends into my game. The way I did it was on the native side (java) I setup my facebook sdk. I succefuly login and pull down my friends list without problems. My issue is that I need to forward this data to the c++ side so I can access the data and bring it into labels etc..
Here I guess some structure of how stuff is happening:
Java native - start activity, login to facebook, get friends -> STRING DATA JNI TO C++ -> CPP parse JSON data with Jannson.
My issue is that if I have a sample data like this:
[
{
"pic_square": "https://www.facebook.com/blah",
"uid": 4654546445,
"name": "somename"
}
]
I can parse that no problem, But in reality what facebook response with is something like this:
{
Response: responseCode: 200,
graphObject: GraphObject{
graphObjectClass=GraphObject,
state={
"data": [
{
"pic_square": "https://www.facebook.com/blah",
"uid": 4654546445,
"name": "somename"
}
]
}
}
}
And with that Jansson fails stating that its not an array (exact error is "error: root is not an array").
Not sure how to handle this. Should I be somehow parsing out the stuff after "data": and then figuring out where to stop correctly or is there a better way.
Thanks!!
What you'll need to do is modify the parsing logic to first handle the Json objects that wrap the data array you're interested in. Although this will require some extra programming, it definitely beats any String manipulation attempts. Unless you're a 100% sure that "[" and "]" will always be part of the response, then I wouldn't be making any assumptions about what you're receiving.
I'm not familiar with Jannson, but you'll want to do some other bits and pieces before handling the data array. Just from looking at the tutorial, it should probably look somewhat like this:
// make request
text = request(url);
// decode json
root = json_loads(text, 0, &error);
// parse "Response"
json_t *response = json_object_get(root, "Response");
json_t *responseCode = json_object_get(response, "responseCode");
int responseCodeValue = json_integer_value(responseCode);
// parse "graphObject"
json_t *graphObject = json_object_get(root, "graphObject");
json_t *graphObjectClass = json_object_get(graphObject, "graphObjectClass");
json_t *state = json_object_get(graphObject, "state");
json_t *data = json_object_get(state, "data");
// iterate over the "data" array, parse the key/values etc.
for(i = 0; i < json_array_size(data); i++) {
json_t *data = json_array_get(root, i);
}
For the sake of this example, I've omitted all type checks (you will want to add those yourself) as well as any cleaning up of memory/variables. Also, please beware of any typos and/or obvious mistakes, as I simply typed this straight into the browser and did not do any compile or runtime checks. I'm sure you'll be able to filter those out on your own.
One thing that I'm curious about is why you've opted for Jannson? I'm guessing because of its support for both Android and iOS? If you're specifically targeting Android, there are lots of other options out there. For example, basic Json support is built into the Android framework, but there's also 3rd party libraries that will enable mapping of Json to Java objects, like GSON and Jackson.

500KB json file parsed in Android environment

I need to find a faster solution to parse a json file with 500KB. the structure is something like
{
"response": {
"code": 0,
"msg": "OK",
"searchparameter": {
"bikes": { … },
"cars": {
"a":{
values[{...}]
},
"b":{},
"c":{},
"d":{},
"e":{},
...
}
}
}
}
I tried gson.fromJson(jsontxt, Response.class), but it causes me like more than 5 mins to parse.. Is there any solutions that is suitable for me? How can I do JSONReader by gson in this case? and would it be helpful? any helps would be appreciated. thanks a lot!!
Try using a streaming API:
For Gson, https://sites.google.com/site/gson/streaming
Or switch to Jackson: Parsing huge JSON object in Android?
you can use Gson itself but to make it useful try it in using Asyntask so you can tell the user to wait by showing progressDialog

Categories

Resources