0

I get this JSON returned and i want to select the metadata "Title" so i can change it to something else.

{
"d": {
    "results": [
        {
            "__metadata": {
                "id": "a8d82500-b17d-43fc-9d03-ee95c498d374",
                "uri": "http:/industrial-ex/sites/tifdemo/_api/Web/Lists(guid'64623cd3-3c4b-4e79-9e94-3e69059903f2')/Items(6)",
                "etag": "\"33\"",
                "type": "SP.Data.EnoviaItem"
            },
            "FirstUniqueAncestorSecurableObject": {
                "__deferred": {
                    "uri": "http:/industrial-ex/sites/tifdemo/_api/Web/Lists(guid'64623cd3-3c4b-4e79-9e94-3e69059903f2')/Items(6)/FirstUniqueAncestorSecurableObject"
                }
            },
            "RoleAssignments": {
                "__deferred": {
                    "uri": "http:/industrial-ex/sites/tifdemo/_api/Web/Lists(guid'64623cd3-3c4b-4e79-9e94-3e69059903f2')/Items(6)/RoleAssignments"
                }
            },
            "AttachmentFiles": {
                "__deferred": {
                    "uri": "http:/industrial-ex/sites/tifdemo/_api/Web/Lists(guid'64623cd3-3c4b-4e79-9e94-3e69059903f2')/Items(6)/AttachmentFiles"
                }
            },
            "ContentType": {
                "__deferred": {
                    "uri": "http:/industrial-ex/sites/tifdemo/_api/Web/Lists(guid'64623cd3-3c4b-4e79-9e94-3e69059903f2')/Items(6)/ContentType"
                }
            },
            "FieldValuesAsHtml": {
                "__deferred": {
                    "uri": "http:/industrial-ex/sites/tifdemo/_api/Web/Lists(guid'64623cd3-3c4b-4e79-9e94-3e69059903f2')/Items(6)/FieldValuesAsHtml"
                }
            },
            "FieldValuesAsText": {
                "__deferred": {
                    "uri": "http:/industrial-ex/sites/tifdemo/_api/Web/Lists(guid'64623cd3-3c4b-4e79-9e94-3e69059903f2')/Items(6)/FieldValuesAsText"
                }
            },
            "FieldValuesForEdit": {
                "__deferred": {
                    "uri": "http:/industrial-ex/sites/tifdemo/_api/Web/Lists(guid'64623cd3-3c4b-4e79-9e94-3e69059903f2')/Items(6)/FieldValuesForEdit"
                }
            },
            "File": {
                "__deferred": {
                    "uri": "http:/industrial-ex/sites/tifdemo/_api/Web/Lists(guid'64623cd3-3c4b-4e79-9e94-3e69059903f2')/Items(6)/File"
                }
            },
            "Folder": {
                "__deferred": {
                    "uri": "http:/industrial-ex/sites/tifdemo/_api/Web/Lists(guid'64623cd3-3c4b-4e79-9e94-3e69059903f2')/Items(6)/Folder"
                }
            },
            "ParentList": {
                "__deferred": {
                    "uri": "http:/industrial-ex/sites/tifdemo/_api/Web/Lists(guid'64623cd3-3c4b-4e79-9e94-3e69059903f2')/Items(6)/ParentList"
                }
            },
            "FileSystemObjectType": 0,
            "Id": 6,
            "ID": 6,
            "ContentTypeId": "0x010100B54C4134C49AE4429841FEE843FEF1D0",
            "Created": "2015-02-11T08:04:27Z",
            "AuthorId": 1073741823,
            "Modified": "2015-02-11T16:37:09Z",
            "EditorId": 1,
            "OData__CopySource": null,
            "CheckoutUserId": null,
            "OData__UIVersionString": "1.0",
            "GUID": "5b5f67c9-5ad6-499c-8799-f91baa7a31df",
            "Title": "test"
        }
    ]
}
}

Here is the Java code i got right now to GET the JSON format.

String url = "http://industrial-ex/sites/tifdemo/_api/web/lists/getbytitle('enovia')/Items";
GetMethod get = new GetMethod(url);
get.setRequestHeader("Accept", "application/json; odata=verbose;");

authentication.getHttpClient().executeMethod(get);
JsonParser p = new JsonParser();
JsonElement jsonElement = p.parse(get.getResponseBodyAsString());
JsonObject asJsonObject = jsonElement.getAsJsonObject();
System.out.println("JSON OBJECT: " + asJsonObject);
String title = asJsonObject.get("d").toString();
System.out.println("---RESULT---: " + title);</code>

2 Answers 2

0

I'm guessing you are using Gson. If so, try this:

String title = asJsonObject.get("d").getAsJsonObject().get("results").getAsJsonArray().get(0).getAsJsonObject().get("Title").getAsString();

It's just about following the layout. You can also do ?$select=Title&$top=1 in your query if you simply want the first items Title as you hint at (even though it sounds odd).

Also note it is probably better to deserialize the result into classes.

EDIT:
Here is an example with classes and deserializing:

public class Result<T> {
    public T d;
}

public class ResultList<T> {
    public List<T> results;
}

public class Item {
    public String Title;
}

...

Gson gson = new Gson();
Result<ResultList<Item>> result = gson.fromJson(yourJsonString, new TypeToken<Result<ResultList<Item>>>(){}.getType());
System.out.println(result.d.results.get(0).Title);
3
  • Now when i got the title, i want to change it and make a post. How should the url look like? I'm posting files to document library with this url: industrial-ex/sites/tifdemo/enovia/FILENAME.txt Where 'enovia' is my document library. I never send metadata. Commented Feb 12, 2015 at 9:56
  • Call MERGE (or PATCH) on the item with Title and __metadata present. e.g., MERGE .../Items(42) with data {"__metadata":{"type":"SP.ListItem"},"Title":"derp"}
    – eirikb
    Commented Feb 12, 2015 at 9:59
  • Sorry i didn't get it.. Commented Feb 12, 2015 at 10:22
0

This is how i make a POST to document library right now..

documentLibrary = sharepoint server url to document library
http://industrial-ex/sites/tifdemo/enovia/

enoviaDocument = local path to a file
D:\tif-destination

if (enoviaDocument.isFile()) { FileInputStream fileInputStream = new FileInputStream(enoviaDocument); PutMethod method = new PutMethod(documentLibrary + enoviaDocument.getName()); RequestEntity requestEntity = new InputStreamRequestEntity(fileInputStream); method.setRequestEntity(requestEntity); Constant.getHttpclient().executeMethod(method); System.out.println("STATUS CODE: " + method.getStatusCode() + " " + "STATUS TEXT: " + method.getStatusText()); }

4
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Benny Skogberg
    Commented Feb 20, 2015 at 14:10
  • @BennySkogberg Hey how did you do that? I tried to do it, but saw only a manual way to create a room, that chat is even linked to the question. I also vaguely remember that long conversations got this "create chat"-link, alas no such link popped up this time.
    – eirikb
    Commented Feb 20, 2015 at 14:16
  • @eirikb I have special privileges since I'm one of four moderators of this site ;-)
    – Benny Skogberg
    Commented Feb 20, 2015 at 14:18
  • Well thanks for the room, much appreciated. Also, wouldn't it be a nice feature if normal users could create such rooms, even before they start a discussion in comments (I for one knew this would drag out quite early, but I didn't know how to make a room - I also knew this answer is actually a question so it is up for deletion so I didn't care that much)
    – eirikb
    Commented Feb 20, 2015 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.